SimpleXML Attributes to Array

前端 未结 5 1011
臣服心动
臣服心动 2020-12-15 05:19

Is there any more elegant way to escape SimpleXML attributes to an array?

$result = $xml->xpath( $xpath );
$element = $result[ 0 ];
$attributes = (array)          


        
5条回答
  •  眼角桃花
    2020-12-15 05:35

    I think you will have to loop through. You can get it into array once you read xml.

     $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
    }
    
    $xmlStr = file_get_contents($xml_file);
    $xmlObj = simplexml_load_string($xmlStr);
    $arrXml = objectsIntoArray($xmlObj);
    
    foreach($arrXml as $attr)
      foreach($attr as $key->$val){
     if($key == '@attributes') ....
    }
    

提交回复
热议问题