2

I have a json feed in a URL that contains following data.

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
[{"ID":1123,"OrderNumber":"1394","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"someone.biz/Home/ShowTemplate/283","ShipDate":"2/28/2015","InHomeDate":"3/2/2015","Quantity":"10,000","Price":"$3,000","CallTracking":"0"},{"ID":1123,"OrderNumber":"1413","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"","ShipDate":"3/30/2015","InHomeDate":"3/31/2015","Quantity":"5,000","Price":"$1,500","CallTracking":"0"},{"ID":1123,"OrderNumber":"1413","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"","ShipDate":"4/13/2015","InHomeDate":"4/14/2015","Quantity":"5,000","Price":"$1,500","CallTracking":"0"}]
</string>

I need to get it and parse it thorough php. But it is giving invalid foreach error with the following code. Can anyone help me on how to show in correctly.

$json = file_get_contents('http://someurl.biz/api/api/1123');

$obj = json_decode($json, true);

foreach($obj as $ob) {
    echo $ob->ID;
}   
LSN
  • 328
  • 2
  • 14

4 Answers4

4

Try as

$json = file_get_contents('http://superiorpostcards.biz/api/api/1123');
$obj = json_decode($json, true);
$array = json_decode($obj, true);
foreach($array as $value){
    echo $value['ID'];
}
Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
1

This works.

As your JSON has become an associative array, you have to make 2 foreach.

  • Top foreach parses the 3 "objects" in '[object1, object2, object3]'
  • Bottom foreach parses each "object" content

    $data = json_decode('[{"ID":1123,"OrderNumber":"1394","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"someone.biz/Home/ShowTemplate/283","ShipDate":"2/28/2015","InHomeDate":"3/2/2015","Quantity":"10,000","Price":"$3,000","CallTracking":"0"},{"ID":1123,"OrderNumber":"1413","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"","ShipDate":"3/30/2015","InHomeDate":"3/31/2015","Quantity":"5,000","Price":"$1,500","CallTracking":"0"},{"ID":1123,"OrderNumber":"1413","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"","ShipDate":"4/13/2015","InHomeDate":"4/14/2015","Quantity":"5,000","Price":"$1,500","CallTracking":"0"}]');
    
     foreach($data as $obj) {
         foreach($obj as $key=>$val) {
            echo $key."->".$val." | ";
         }
     }   
    

Yes it is simpler with JS. But php "json" is not a JS object, it is an array of associative arrays.

3pic
  • 1,188
  • 8
  • 26
  • thanks for your effort.. it work when i use the json feed instead the url but i wanted to work with URL. Already got the solution. Thank you again :) – LSN Jul 31 '15 at 13:31
  • Welcome, enjoy coding. Gimme a +1 and a check, if my answer is correct. That will drive readers. Cheers (btw, when is the best time to visit SriLanka ?) – 3pic Jul 31 '15 at 13:40
0
$my_array_for_parsing = json_decode(/** put the json here */);

this gives you the JSon as php associative array.


$my_array_for_parsing = json_decode($json);
foreach ($my_array_for_parsing as $name => $value) {
    // This will loop three times:
    //     $name = a
    //     $name = b
    //     $name = c
    // ...with $value as the value of that property
}
3pic
  • 1,188
  • 8
  • 26
  • Where did I not ? In php, you dont have a JSON, you only can get an **associative array**. `json_decode`do the conversion. JSon is a `javascript` object. "php json" is an associative array. – 3pic Jul 31 '15 at 13:14
  • 1
    The part about the foreach output not being correct. The OP returns an array with JSON, not objects which he is trying to output. – Jay Blanchard Jul 31 '15 at 13:15
  • i post a new answer. Working – 3pic Jul 31 '15 at 13:25
0

If second parameter of json_decode is set to true, your json will be transformed in associative array instead of object. Try this:

$obj = json_decode($json, false);

foreach($obj as $ob) {
    echo $ob->ID;
}   
Kristian Vitozev
  • 5,791
  • 6
  • 36
  • 56
  • didnt help Warning: Invalid argument supplied for foreach() in – LSN Jul 31 '15 at 13:14
  • Make sure that `$obj` is not empty. What is the output of `var_dump( $obj );` – Kristian Vitozev Jul 31 '15 at 13:15
  • string(668) "[{"ID":1123,"OrderNumber":"1394","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"someone.biz/Home/ShowTemplate/283","ShipDate":"2/28/2015","InHomeDate":"3/2/2015","Quantity":"10,000","Price":"$3,000","CallTracking":"0"},{"ID":1123,"OrderNumber":"1413","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"","ShipDate":"3/30/2015","InHomeDate":"3/31/2015","Quantity":"5,000","Price":"$1,500","CallTracking":"0"}]" – LSN Jul 31 '15 at 13:17
  • Can you paste a whole code (updated) in your question? – Kristian Vitozev Jul 31 '15 at 13:19