Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按特定顺序输出数据的简单xml_Php_Xml_Simplexml - Fatal编程技术网

Php 按特定顺序输出数据的简单xml

Php 按特定顺序输出数据的简单xml,php,xml,simplexml,Php,Xml,Simplexml,我有一个简单的问题,但我在网上找不到任何有用的信息 我有一个xml文件: <market id="27719037" name="Wolves v Leeds"> <participant name="home" id="121081062" odds="7/2"/> <participant name="Draw" id="121081062" odds="7/2"/> <participant name="away" id="121081062" o

我有一个简单的问题,但我在网上找不到任何有用的信息

我有一个xml文件:

<market id="27719037" name="Wolves v Leeds">
<participant name="home" id="121081062" odds="7/2"/>
<participant name="Draw" id="121081062" odds="7/2"/>
<participant name="away" id="121081062" odds="7/2"/>
</market>

<market id="27719037" name="Liverpool v Fulham">
<participant name="Draw" id="121081062" odds="7/2"/>
<participant name="home" id="121081062" odds="7/2"/>
<participant name="away" id="121081062" odds="7/2"/>
</market>

因为每个市场游戏的XML输出顺序不同,所以我不能简单地依赖于按顺序输出

有什么方法可以让我全部退出:

主场优势 赔率 赔率

提前谢谢

richard

您可以这样做:

$xml= simplexml_load_file('temp.xml');

$xmlarr = array();

$i=0;
foreach($xml as $market) {
foreach($market as $participant){
    $name =  (string)$participant['name'];
    //to store in pairs of 'home odds' 'draw odds' 'away odds'
    $xmlarr[$i][$name] = (string)$participant['odds'];

 }
    ++$i;    
  } 
现在您有了
$xmlarr
数组,其结构如下所示:

array(2) {
  [0]=>
  array(3) {
    ["home"]=>
    string(3) "7/2"
    ["Draw"]=>
    string(3) "7/2"
    ["away"]=>
    string(3) "7/2"
  }
  [1]=>
  array(3) {
    ["Draw"]=>
    string(3) "7/2"
    ["home"]=>
    string(3) "7/2"
    ["away"]=>
    string(3) "7/2"
  }
}
foreach ($xmlarr as $element) {
 echo "home : ".$element ['home']."<br>";
 echo "Draw : ".$element ['Draw']."<br>";
 echo "away : ".$element ['away']."<br>"; 
 }
您可以按如下所示的顺序访问它们:

array(2) {
  [0]=>
  array(3) {
    ["home"]=>
    string(3) "7/2"
    ["Draw"]=>
    string(3) "7/2"
    ["away"]=>
    string(3) "7/2"
  }
  [1]=>
  array(3) {
    ["Draw"]=>
    string(3) "7/2"
    ["home"]=>
    string(3) "7/2"
    ["away"]=>
    string(3) "7/2"
  }
}
foreach ($xmlarr as $element) {
 echo "home : ".$element ['home']."<br>";
 echo "Draw : ".$element ['Draw']."<br>";
 echo "away : ".$element ['away']."<br>"; 
 }
当您访问
['home']
时,您将获得与其相关的
奇数
。同样适用于
['Draw']
['away']