Reputation: 21
PHP From the xml file below I have to pickup 2 purchase prices including 2 quantities and put them into a table. The xml you see below. I can not get the needed vars out of the array.
<?xml version="1.0" encoding="UTF-8"?><products>
<Product>
<ParentName>22.01.14.10</ParentName>
<Name>3040-100</Name>
<Prices>
<Price min-qty="1" currency="EUR">6,3200</Price>
<Price min-qty="12" currency="EUR">5,3200</Price>
<Price min-qty="24" currency="EUR">4,7200</Price>
</Prices>
</Product>
<Product>
<ParentName>22.01.10</ParentName>
<Name>PDS1C</Name>
<Prices>
<Price min-qty="1" currency="EUR">1,9565</Price>
<Price min-qty="10" currency="EUR">1,6828</Price>
<Price min-qty="20" currency="EUR">1,4828</Price>
</Prices>
</Product>
<Product>
<ParentName>22.01.14</ParentName>
<Name>P1017</Name>
<Prices>
<Price min-qty="1" currency="EUR">4,9337</Price>
<Price min-qty="20" currency="EUR">3,9699</Price>
</Prices>
</Product>
</products>
With this I fill the prices array:
foreach($product->{'Prices'}->children() as $key => $price)
{
$prices[ "" . $price->attributes()->{'min-qty'}] = mysql_real_escape_string(str_replace(',','.',"" . $price[0]));
}
ksort($prices); // sort array
Here the results:
Array
(
[1] => 6.3200
[12] => 5.3200
[24] => 4.7200
)
Array
(
[1] => 1.9565
[10] => 1.6828
[20] => 1.4828
)
Array
(
[1] => 4.9337
[20] => 3.9699
)
Now I have to fill the vars to put the vars into the table:
$qfirst = ''; // should be 1
$pfirst = ''; // should be 6.3200
$qsec = ''; // should be 12
$psec = ''; // should be 5.3200
What I try I do not get the data into the 4 vars.
Upvotes: 0
Views: 60
Reputation: 95103
Change
$prices[ "" . $price->attributes()->{'min-qty'}] = mysql_real_escape_string(str_replace(',','.',"" . $price[0]));
To
$prices[] = array($price->attributes()->{'min-qty'},mysql_real_escape_string(str_replace(',', '.', "" . $price[0])));
Then
foreach(array_map(null,array("first","sec","third"),$prices) as $blend)
{
${"q". $blend[0]} = $blend[1][0];
${"p". $blend[0]} = $blend[1][1];
}
var_dump($qfirst,$pfirst,$qsec,$psec) ; // it would create this variables
OR
reset($prices);
foreach(array("first","sec","third") as $blend)
{
${"q". $blend} = key($prices);
${"p". $blend} = current($prices);
next($prices);
}
var_dump($qfirst,$pfirst,$qsec,$psec) ;
Upvotes: 2
Reputation: 24551
This simulates the first two rounds of a foreach
loop and gives the desired result:
reset($prices);
$qfirst = key($prices);
$pfirst = current($prices);
next($prices);
$qsec = key($prices);
$psec = current($prices);
Upvotes: 0
Reputation: 6002
you can use
for printing all the keys values. => print_r(array_keys($array));
for printing values => print_r(array_values($array));
Upvotes: 0