如何从PHP中的XML循环foreach获取数组数据

时间:2019-04-17 13:54:41

标签: php xml

我有这样的XML货币输出:

<ValCurs Date="13.04.2019" name="Foreign Currency Market">
    <Valute ID="R01010">
      <NumCode>036</NumCode>
      <CharCode>AUD</CharCode>
      <Nominal>1</Nominal>
      <Name>Австралийский доллар</Name>
      <Value>46,0781</Value>
    </Valute>
    <Valute ID="R01020A">
      <NumCode>944</NumCode>
      <CharCode>AZN</CharCode>
      <Nominal>1</Nominal>
      <Name>Азербайджанский манат</Name>
      <Value>38,0295</Value>
    </Valute>
</ValCurs>

我想通过提供<Name>来获得<Value><Valute><Valute> ID

我在php中的这个foreach循环中尝试过:

$xml = simplexml_load_string($url) or die ("error cannot create object");
$results = $xml->ValCurs;
$res=count($results);
echo " currency : $res <br><br>";
$output=array();
$keyword = array();
foreach($xml->ValCurs as $nodes){
$res=count($nodes);
    foreach ($nodes->Valute as $key =>$node) {
      $output=$node->NumCode;
      echo $output;
      echo "<br><br>";
      echo $keyword[]=substr($output,66);
      echo "<br><br>";  
    }
}
    echo"<hr>";
    echo $keyword[0];

结果是货币为0

3 个答案:

答案 0 :(得分:1)

这很简单:

$s = '<ValCurs Date="13.04.2019" name="Foreign Currency Market">
    <Valute ID="R01010">
      <NumCode>036</NumCode>
      <CharCode>AUD</CharCode>
      <Nominal>1</Nominal>
      <Name>Австралийский доллар</Name>
      <Value>46,0781</Value>
    </Valute>
    <Valute ID="R01020A">
      <NumCode>944</NumCode>
      <CharCode>AZN</CharCode>
      <Nominal>1</Nominal>
      <Name>Азербайджанский манат</Name>
      <Value>38,0295</Value>
    </Valute>
</ValCurs>';

// Note, that `simplexml_load_string` uses a STRING, 
// if `$url` is url to some resource, first get data from 
// this resource and then load this data to SimpleXML
$xml = simplexml_load_string($s) or die ("error cannot create object");
foreach($xml as $node){
      echo $node->Name;
      echo "<br><br>";
      echo $node->Value;
      echo "<br><br>";
      echo $node['ID'];
      echo "<br><br>";
}

提琴here

答案 1 :(得分:0)

<?php
function object2array($object) { return @json_decode(@json_encode($object),1); }
$xmldata = <<<XML
<ValCurs Date="13.04.2019" name="Foreign Currency Market">
    <Valute ID="R01010">
      <NumCode>036</NumCode>
      <CharCode>AUD</CharCode>
      <Nominal>1</Nominal>
      <Name>Австралийский доллар</Name>
      <Value>46,0781</Value>
    </Valute>
    <Valute ID="R01020A">
      <NumCode>944</NumCode>
      <CharCode>AZN</CharCode>
      <Nominal>1</Nominal>
      <Name>Азербайджанский манат</Name>
      <Value>38,0295</Value>
    </Valute>
</ValCurs>
XML;
$xml = simplexml_load_string($xmldata) or die ("error cannot create object");
$xml_array=object2array($xml);
echo'<pre>';
print_r($xml_array);
foreach($xml_array['Valute'] as $key=>$value){
    echo $value['@attributes']['ID'].'<br>';
    echo $value['Name'].'<br>';
    echo $value['Value'].'<br>';
}

样本输出

R01010
Австралийский доллар
R01020A
Азербайджанский манат

答案 2 :(得分:0)

步骤:

1)通过simplexml_load_string()

将XML转换为字符串

2)json_encode()字符串用于解码。

3)json_deocde():以数组形式返回。

4)遍历数组

5)将循环值附加到新创建的数组。

<?php
$xml = '<?xml version = "1.0" encoding = "utf-8"?>
<ValCurs Date="13.04.2019" name="Foreign Currency Market">
    <Valute ID="R01010">
      <NumCode>036</NumCode>
      <CharCode>AUD</CharCode>
      <Nominal>1</Nominal>
      <Name>Австралийский доллар</Name>
      <Value>46,0781</Value>
    </Valute>
    <Valute ID="R01020A">
      <NumCode>944</NumCode>
      <CharCode>AZN</CharCode>
      <Nominal>1</Nominal>
      <Name>Азербайджанский манат</Name>
      <Value>38,0295</Value>
    </Valute>
</ValCurs>';

$arr = [];
$array = json_decode(json_encode(simplexml_load_string($xml)),true);
if ( ! empty($array['Valute'])) {
 $i=0;
 foreach ($array['Valute'] as $elem) {
   $arr[$i]['NumCode'] = $elem['NumCode'];
   $arr[$i]['CharCode'] = $elem['CharCode'];
   $arr[$i]['Nominal'] = $elem['Nominal'];
   $arr[$i]['Name'] = $elem['Name'];
   $arr[$i]['Value'] = $elem['Value'];
  ++$i;
 }
}
echo '<pre>';print_r($arr);echo '</pre>';

输出:

Array
(
    [0] => Array
        (
            [NumCode] => 036
            [CharCode] => AUD
            [Nominal] => 1
            [Name] => Австралийский доллар
            [Value] => 46,0781
        )

    [1] => Array
        (
            [NumCode] => 944
            [CharCode] => AZN
            [Nominal] => 1
            [Name] => Азербайджанский манат
            [Value] => 38,0295
        )

)

Working Online Code: