-2

how correct read array was from xml with json convert in php array?

     <php?
    $xmlfile = file_get_contents('data.xml');
    $ob= simplexml_load_string($xmlfile);
    $json  = json_encode($ob);
    $configData = json_decode($json, true);
    foreach($configData["id"] as $configData)
    {
    echo $configData["name"];
    echo $configData["image"];
     echo $configData["id"];

    }

?>

Warning: Invalid argument supplied for foreach in /data.php on line 10

Ulvis
  • 1
  • 1
  • Check this out: https://stackoverflow.com/questions/6167279/converting-a-simplexml-object-to-an-array – Goranov May 15 '18 at 14:04

2 Answers2

0

Try using simplexml_load_file function. I have always found success in using this whenever you are dealing with xml files.

Kishen Nagaraju
  • 2,062
  • 9
  • 17
0

If you want a list of the ids, you should stick to using SimpleXML...

$xmlfile = simplexml_load_file("data.xml");

foreach ( $xmlfile->smarza as $data )   {
    echo $data->id.PHP_EOL;
}

This loads the file using simplexml_load_file() and then loops in the foreach() it goes over each <smarza> element (using $xmlfile->smarza). The access the <id> element using $data->id.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55