简体   繁体   English

从数组中获取数据的问题

[英]problems getting data from array

PHP From the xml file below I have to pickup 2 purchase prices including 2 quantities and put them into a table. PHP从下面的xml文件中我必须提取2个购买价格,包括2个数量并将它们放入表中。 The xml you see below. 您在下面看到的xml。 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. 我尝试的是我没有将数据输入4个变量。

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

you can use 您可以使用

for printing all the keys values. 用于打印所有键值。 => print_r(array_keys($array)); => print_r(array_keys($array));

for printing values => print_r(array_values($array)); 用于打印值=> print_r(array_values($ array));

This simulates the first two rounds of a foreach loop and gives the desired result: 这模拟了foreach循环的前两轮并给出了所需的结果:

reset($prices);
$qfirst = key($prices);
$pfirst = current($prices);

next($prices);
$qsec = key($prices);
$psec = current($prices);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM