Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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 SimpleXMLElement中提取数组_Php_Json_Indexing_Simplexml_Php 7.1 - Fatal编程技术网

从PHP SimpleXMLElement中提取数组

从PHP SimpleXMLElement中提取数组,php,json,indexing,simplexml,php-7.1,Php,Json,Indexing,Simplexml,Php 7.1,我想从PHP SimpleXmlElement中提取一个数组。 原始SimpleXMLElement的结构如下所示: object(SimpleXMLElement)[226] public 'GetLeadDetailResponse' => object(SimpleXMLElement)[239] public 'GetLeadDetailResult' => object(SimpleXMLElement)[240]

我想从PHP SimpleXmlElement中提取一个数组。 原始SimpleXMLElement的结构如下所示:

object(SimpleXMLElement)[226]
  public 'GetLeadDetailResponse' => 
    object(SimpleXMLElement)[239]
      public 'GetLeadDetailResult' => 
        object(SimpleXMLElement)[240]
          public 'LeadStat' => 
            array (size=4294)
xmlElement存储在名为
$parser
的VARABLE中,我想一直到'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_编码,因此我尝试了以下方法:

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
实际上不是数组,而是一个iterable元素。要获取
LeadStat
中的所有项目,需要对其进行迭代

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

啊,我明白了。我被假阵法骗了。谢谢,我的应用程序现在可以正常工作了。