0
{"cities":[{"city":{"id":1,"name":"Bangalore","status":"Active"}},
{"city":{"id":2,"name":"Mysore","status":"Active"}},... ]}

The above is the JSON format and this is how I fetch from JSON as shown below,

$city=$data['cities'];
$citycount=count($city);
for($i=0;$i<$citycount;$i++)
{
  echo $data['cities'][$i]['city']['id'];
}

Now How to fetch JSON values when the format is like below

{"result":[["id","name","status"],[1,"Bangalore","Active"],[2,"Mysore","Active"],... ]} 

I'm using core PHP to fetch data from JSON with the method file_get_contents and json_decode

Kirataka
  • 484
  • 8
  • 26
  • Is there anything that you have tried to fetch values? – Mubashar Abbas Apr 18 '16 at 06:12
  • @PaulCrovella Ya but I'm trying for other format which I specified in above. Trying in different format. – Kirataka Apr 18 '16 at 06:13
  • @MubasharAbbas I'm trying to fetch values from new JSON format – Kirataka Apr 18 '16 at 06:14
  • json_decode will successfully decode it into a php object. Next you have add some checks for the first array because it has headers as well as data.. after that.. you need a loop for the rest of arrays to extract data. – Mubashar Abbas Apr 18 '16 at 06:19
  • @MubasharAbbas It has header but no data in first array. Data are in next array as shown in the SECOND format. – Kirataka Apr 18 '16 at 06:24
  • if you look carefully, then the first array has as first element, an array with all the header items. and then the second, third, fourth elements are the values.. – Mubashar Abbas Apr 18 '16 at 06:26

3 Answers3

2

Try it like this.

$result = json_decode($newjson);
$result = $result->result;
$data = [];
$headers= $result[0]; // get the first row.
foreach($result as $key => $row) {
   // ignore the headers row.
   if($key != 0) {
       array_push($data, [
          $headers[0] => $row[0],
          $headers[1] => $row[1],
          $headers[2] => $row[2]
       ]);
   }
}

To show these values in form elements like 'dropdown' use this.

<select name="somename">
   <?php foreach($data as $item) { ?>
      <option><?= $item['name'] ?></option>
   <?php } ?>
</select>
halfer
  • 19,824
  • 17
  • 99
  • 186
Mubashar Abbas
  • 5,536
  • 4
  • 38
  • 49
1

Try this

$json = '{"result":[["id","name","status"],[1,"Bangalore","Active"],[2,"Mysore","Active"]]} ';
$result = json_decode($json,true);
$dataArray = [];
foreach($result['result'] as $key => $value) {

   if($key != 0) {
       array_push($dataArray, [
          $result['result'][0][0] => $value[0],
          $result['result'][0][1] => $value[1],
          $result['result'][0][2] => $value[2]
       ]);
   }
}
print_r($dataArray);
exit;
Ninju
  • 2,522
  • 2
  • 15
  • 21
0

Here you have a more dynamic method, does not matter what headers you have it will combine automatically, but is very important that each row need to contain the same array length.

$data = '{"result":[["id","name","status"],[1,"Bangalore","Active"],[2,"Mysore","Active"]]}';

$_data = json_decode($data);
if(!empty($_data)){
    $i = 0; //I know that the first row is the header
    foreach($_data->result as $row){
        if($i++ == 0){
            $header = $row;
            continue;
        }
        $newData[] = array_combine($header, $row);
    }
    echo '<pre>';
    print_r($newData);
}
ndAR
  • 371
  • 2
  • 4
  • 15