PHP convert XML to JSON

前端 未结 20 1464
生来不讨喜
生来不讨喜 2020-11-22 06:25

I am trying to convert xml to json in php. If I do a simple convert using simple xml and json_encode none of the attributes in the xml show.

$xml = simplexml         


        
20条回答
  •  终归单人心
    2020-11-22 07:13

    I figured it out. json_encode handles objects differently than strings. I cast the object to a string and it works now.

    foreach($xml->children() as $state)
    {
        $states[]= array('state' => (string)$state->name); 
    }       
    echo json_encode($states);
    

提交回复
热议问题