dear all,
i want to parse this url:

to get all image, selectedImage, and url value.

and my php syntax is:

$url1 = "";
$url = file_get_contents("$url1");
$arr = json_decode($url,true);
foreach($arr['menu']['image'] as $item) {
   echo "image: ". $item[0] ."<br>";
   echo "image_selected: ". $item[1] ."<br>";
   echo "url: ". $item[2] ."<br>";

but i got Warning: Invalid argument supplied for foreach(), so how to get expected results?

Member Avatar


Try this...

$url1 = "";
$url = file_get_contents($url1);
$pos = strpos($url,'{');
$json = substr($url,$pos);
$arr = json_decode($json,true);

foreach($arr['menu'] as $item) {
   echo "image: ". $item['image'] ."<br>";
   echo "image_selected: ". $item['selectedImage'] ."<br>";
   echo "url: ". $item['url'] ."<br><br>";

There was non-json output at the start of the file - needed to be stripped.

thank you, mr.diafol
i did not know it was non-json output.
i use json_decode,because i think it was json output.

thanks anyway.

Your file isn't valid json
to create it you have to put all output through json_encode
Also recoment using header("Content-type: text/json"); to create a true json-file