Search code examples
phpweb-serviceswebexchangewebservices

How to get properties through loop from a REST JSON response using Httpfull in PHP and how to show in grid


As in the URL, I don't want to give a limit, I want to show in loop
[0]. I don't want to give [0] or [1] etc. I want to do it in a loop.

 http://buildout.com/api/v1/d7d8b6a8cbd23c1f69e3a327b32c8217d6bea635/properties.json?limit=2

 <?php    
     $url='my url';
     $res = \Httpful\Request::get($url)->expectsJson()->send();
     $data = json_decode($res,true);
     $name1 =  $data['properties'][0]['broker_id'];  // I WANT LOOP HERE INSTED OF [0]
     echo $city1;  
?>

Solution

  • You can loop through the response data dynamically to get each datum without specifying the index as follows:

    foreach ($data['properties'] as $key=>$val) {       
        var_dump($key);
    }
    

    The $key can be:

    • First $data['properties'][0];
    • Next $data['properties'][1];
    • and so on...