0

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.

3
  • 1
    $product->{'Prices'}->children() - wait, that syntax is valid? Commented Feb 12, 2013 at 13:16
  • 1
    @slugonamission It is unusual but it is perfectly valid. Commented Feb 12, 2013 at 13:25
  • @MathieuImbert - fair enough, I'd just never seen that syntax before and it's very weird. Commented Feb 12, 2013 at 13:26

3 Answers 3

2

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) ; 
Sign up to request clarification or add additional context in comments.

7 Comments

Love this! Dynamic vars in PHP make it really flexible although a pain to debug :)
Baba; 100% score! Thanks. Finally a solution.
@user1575807 - Give him a +1 whilst your at it ;) It's a nice solution.
@webnoob you can warp it in a class making it easy to debug and access
@Baba - I agree but from a beginners point of view where they would be looking for $myVar = "someVal" then it's something very different :)
|
0

you can use

for printing all the keys values. => print_r(array_keys($array));

for printing values => print_r(array_values($array));

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.