PHP find min value of key in xml
I'm new in PHP, so I need some help because I cant find my error. Here is
my xml file:
<children>
<child id="A">
<link id="C" age="10" />
<link id="B" age="13" />
<link id="H" age="12" />
</child>
<child id="B">
<link id="C" age="10" />
<link id="D" age="50" />
</child>
<child id="C">
<link id="I" age="50" />
</child>
</children>
Here is my php code:
$xml = simplexml_load_file('data.xml');
$arr = (array) $xml;
function find_youngest($task_1, $task_2) {
if ($task_1['age'] === $task_2['age']) {return 0;}
return $task_1['age'] - $task_2['age'];
}
foreach ($arr as &$key) {
usort($key, 'find_youngest');
}
var_dump($arr[0]);
echo '<pre>'; print_r($arr); echo '</pre>';
I want to get youngest people in each element and the result should be
like this:
<link id1="A" id2="C" />
<link id1="B" id2="C" />
<link id1="C" id2="I" />
No comments:
Post a Comment