XML URL到PHP数组中

时间:2016-10-15 00:28:17

标签: php xml

很长一段时间的搜索者......第一篇帖子......我被困了......

以下是我的xml链接示例:

<response uri="/crm/private/xml/Deals/getRecords">  
  <result>
    <Deals>
      <row no="1">
        <FL val="DEALID">1338246000003618134</FL>
        <FL val="Deal Name">Again Holdings</FL>
      </row>
      <row no="2">
        <FL val="DEALID">1338246000003599435</FL>
        <FL val="Deal Name">Yankee Doodle</FL>
      </row>
......

我正在尝试创建一个循环来检索“DEALID”和“Deal Name”。 我在这个循环中取得了一些成功,获得了正确的记录数:

<?php
$result = simplexml_load_file($xml_link) or die("Error: Cannot create object");
foreach($result->result->Deals->row as $row) {
   var_dump($row);
   echo "<br>NEXT ROW<br>";
}
?>

现在我无法找到一种方法来提取每个记录的“DEALID”和“Deal Name”的值......任何帮助都会很棒。

〜一个有抱负的开发者,但仍然是新手......

3 个答案:

答案 0 :(得分:0)

试试这个:

<?php
$result = simplexml_load_file($xml_link) or die("Error: Cannot create object");
$newArray = [];
foreach($result->result->Deals->row as $row) {
   $newRow = [];
   foreach($row as $key => $value) {
       $newRow[$key] = $value;
    }
    $newArray[] = $newRow;
}
?>

答案 1 :(得分:0)

关于代码的Cringe是真实的,但有时它会马虎......但是它有效......即将在一分钟内尝试Xanfars的建议。

&#13;
&#13;
<?php
$xml_link = "https://not_the_link.xml )";

$result = simplexml_load_file($xml_link) or die("Error: Cannot create object");

$Project_Name = array();
$Project_ID = array();
$record_count = 0;

foreach($result->result->Deals->row as $row) {
	$fl_count = 0;	
	foreach($row->FL as $record) {
		if ($fl_count == 0){
			$Project_ID[$record_count] = $record;
		}
		if ($fl_count == 1){
			$Project_Name[$record_count] = $record;	
		}
		$fl_count=1;
	}
	echo 'Project Name : '.$Project_Name[$record_count].'<br>';
	echo 'Project ID : '.$Project_ID[$record_count].'<br>-------------------<br>';
	$record_count = $record_count+1;
}
Echo $record_count;
?>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

看看这是否更好:)

$result = simplexml_load_file($xml_link) or die("Error: Cannot create object");
$newArray = [];
foreach($result->result->Deals->row as $row) {
   $newRow = [];
   foreach($row as $value) {
       $key = $value->attributes();
       $newRow[$key->val] = $value;
    }
    $newArray[] = $newRow;
}

编辑: 我现在不得不在测试环境中改变这个抱歉