dumb2champ Posted January 14, 2016 Share Posted January 14, 2016 Hello... I want to calculate the value from 2 difference files using simplexml load file... But the result not as expected.. My output should be 7 and 5 where 10 - 7 and 8 - 3. My first xml files. <?xml version="1.0" encoding="UTF-8"?> <items> <item> <q_id>4131001</q_id> <value>10</value> </item> <item> <q_id>4232002</q_id> <value>8</value> </item> </items> My second xml files. <?xml version="1.0" encoding="UTF-8"?> <items> <item> <q_id>4131001</q_id> <value>7</value> </item> <item> <q_id>4232002</q_id> <value>3</value> </item> </items> My scripts. <?php $xml = simplexml_load_file('q1.xml'); $xml2 = simplexml_load_file('q2.xml'); foreach ($xml -> item as $item) { $value1 = $item->value; echo $item->q_id.'<br />'; //echo $value1.'<br />'; foreach ($xml2 -> item as $item2) { $value2 = $item2->value; $total = $value1 - $value2; echo $total.'<br />'; } } ?> The result should be 7 and 5 where 10 - 7 and 8 - 3 but there is another unknows value which is 3 and 1... How to solve the issue.. Thank for any help.. Quote Link to comment https://forums.phpfreaks.com/topic/300330-simplexml-calculate-2-value-from-2-files/ Share on other sites More sharing options...
dumb2champ Posted January 14, 2016 Author Share Posted January 14, 2016 sory the result should be 3 and 5...sory Quote Link to comment https://forums.phpfreaks.com/topic/300330-simplexml-calculate-2-value-from-2-files/#findComment-1529662 Share on other sites More sharing options...
Barand Posted January 14, 2016 Share Posted January 14, 2016 That's because you have nested the loops. Do something like this $xml1 = simplexml_load_file('q1.xml'); $xml2 = simplexml_load_file('q2.xml'); $a1 = []; foreach ($xml1->xpath('//item') as $i) { $a1[(string)$i->q_id] = (int)$i->value; } $a2 = []; foreach ($xml2->xpath('//item') as $i) { $a2[(string)$i->q_id] = (int)$i->value; } foreach ($a1 as $id => $v) { echo $id . '<br>'; $result = $v - $a2[$id]; echo $result . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/300330-simplexml-calculate-2-value-from-2-files/#findComment-1529670 Share on other sites More sharing options...
dumb2champ Posted January 14, 2016 Author Share Posted January 14, 2016 I got Notice: Undefined offset: 4131001 Output 4131001Notice: Undefined offset: 4131001 on line 171042320025 Quote Link to comment https://forums.phpfreaks.com/topic/300330-simplexml-calculate-2-value-from-2-files/#findComment-1529672 Share on other sites More sharing options...
Barand Posted January 14, 2016 Share Posted January 14, 2016 Have you checked the xml files - is 4131001 in both of them? Quote Link to comment https://forums.phpfreaks.com/topic/300330-simplexml-calculate-2-value-from-2-files/#findComment-1529673 Share on other sites More sharing options...
dumb2champ Posted January 14, 2016 Author Share Posted January 14, 2016 Thanks Barrand.. One more question..i dont quite understand the codes which is: foreach ($xml2->xpath('//item') as $i) { $a1[(string)$i->q_id] = (int)$i->value;} If i want to add another object say, <q_name>This is Q1<q_name> after <q_id> which is like this: <item><q_id>4131001</q_id> <q_name>This is Q1<q_name> <-----<value>7</value></item> How to edit the scripts? Quote Link to comment https://forums.phpfreaks.com/topic/300330-simplexml-calculate-2-value-from-2-files/#findComment-1529674 Share on other sites More sharing options...
dumb2champ Posted January 14, 2016 Author Share Posted January 14, 2016 What i mean was to output <q_name> object..thanks Quote Link to comment https://forums.phpfreaks.com/topic/300330-simplexml-calculate-2-value-from-2-files/#findComment-1529675 Share on other sites More sharing options...
Barand Posted January 15, 2016 Share Posted January 15, 2016 foreach ($xml2->xpath('//item') as $i) { $a1[(string)$i->q_id] = (int)$i->value; } the code scans all the items in the xml data and stores them in an array with the id as the key. If you add "q_name" then the values will be changed to arrays containing the name and the value $xml1 = simplexml_load_file('q1.xml'); $xml2 = simplexml_load_file('q2.xml'); $a1 = []; foreach ($xml1->xpath('//item') as $i) { $a1[(string)$i->q_id] = ['name' => (string)$i->q_name, 'value' => (int)$i->value]; } $a2 = []; foreach ($xml2->xpath('//item') as $i) { $a2[(string)$i->q_id] = ['name' => (string)$i->q_name, 'value' => (int)$i->value]; } foreach ($a1 as $id => $v) { echo $id . '<br>'; echo (string)$v['name'] . '<br>'; $result = $v['value'] - $a2[$id]['value']; echo $result . '<br><br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/300330-simplexml-calculate-2-value-from-2-files/#findComment-1529720 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.