Jump to content

Recommended Posts

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.

0cK6Cng.png?1

 

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..

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>'; 
}

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?

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>'; 
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.