从PHP SimpleXMLElement中提取数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从PHP SimpleXMLElement中提取数组相关的知识,希望对你有一定的参考价值。

我想从php SimpleXmlElement中拉出一个数组。原始的SimpleXMLElement结构如下:

object(SimpleXMLElement)[226]
  public 'GetLeadDetailResponse' => 
    object(SimpleXMLElement)[239]
      public 'GetLeadDetailResult' => 
        object(SimpleXMLElement)[240]
          public 'LeadStat' => 
            array (size=4294)

xmlElement存储在一个名为$parser的变量中,我希望一直到'LeadStat'所以我索引为:

$leadStatInfo = $parser->GetLeadDetailResponse->GetLeadDetailResult;

这返回给我这个对象:

object(SimpleXMLElement)[4534]
  public 'LeadStat' => 
    array (size=4294)
      0 => 
        object(SimpleXMLElement)[240]
          public 'AdvertiserName' => string 'Modn Grp' (length=12)
          public 'AdvertiserId' => string '4539234' (length=6)
          public 'Timestamp' => string '2017-08-14T15:23:28.91' (length=22)
          public 'Type' => string 'Conversation' (length=15)
          public 'VisitId' => string '1442535353670' (length=9)
          public 'Attempt' => string '0' (length=1)
          public 'AgentName' => string 'Jack A' (length=6)
          public 'CustomerName' => string 'John Harris' (length=11)
          public 'CustomerEmail' => string 'harris@modn.com' (length=25)
          public 'CustomerPhone' => 
            object(SimpleXMLElement)[4535]
              ...
          public 'CustomerAddress' => 
            object(SimpleXMLElement)[4536]
              ...
          public 'NumberOfMessages' => string '12' (length=2)
      1 => 
        object(SimpleXMLElement)[4533]
          public 'AdvertiserName' => string 'Modn Grp' (length=12)
          public 'AdvertiserId' => string '4539234' (length=6)
          public 'Timestamp' => string '2017-08-14T17:21:11.157' (length=23)
          public 'Type' => string 'Conversation' (length=15)
          public 'VisitId' => string '37836763725' (length=9)
          public 'Attempt' => string '0' (length=1)
          public 'CustomerAddress' => 
            object(SimpleXMLElement)[4536]
              ...
          public 'NumberOfMessages' => string '0' (length=1)
      more elements...

现在我想得到LeadStat数组和json_encode它所以我尝试了以下内容:

json_encode($leadStatInfo->LeadStat)

遗憾的是,这只返回了一个对象而不是数组中的所有对象。

{
  "AdvertiserName":"Modn Grp",
  "AdvertiserId":"4539234",
  "Timestamp":"2017-08-14T15:23:28.91",
  "Type":"Conversation",
  "VisitId":"1442535353670",
  "Attempt":"0",
  "AgentName":"Jack A",
  "CustomerName":"John Harris",
  "CustomerEmail":"harris@modn.com",
  "CustomerPhone":{},
  "CustomerAddress":{},
  "NumberOfMessages":"12"
}

如何拉出整个阵列?

这是因为数组中的对象没有相同的属性吗?

答案

在SimpleXML中,$leadStatInfo->LeadStat实际上不是一个数组,而是一个可迭代的元素。要获得LeadStatyou中的所有项目需要迭代它。

$leadStatArray = array();
foreach($leadStatInfo->LeadStat as $leadStat) {
    $leadStatArray[] = $leadStat;
}
json_encode($leadStatArray);

以上是关于从PHP SimpleXMLElement中提取数组的主要内容,如果未能解决你的问题,请参考以下文章

PHP中SimpleXMLElement对象字符编码

如何在 PHP 中回显此 SimpleXMLElement 中的 OK 属性? [复制]

解决PHP-问题:Class 'SimpleXMLElement' not found in

PHP SimpleXMLElement xpath

在 puphpet PHP 5.6 上找不到类“SimpleXMLElement”

PHP:SimpleXMLElement() 类和 SimpleXML_load_string() 有啥区别?