1

I have a question regarding on json.. I tried to get an api from other site and downloaded it as json file..I've already decoded this file using this code below..

$str = file_get_contents('../trelloApi.json');
$json = json_decode($str, true);
echo '<pre>' . print_r($json, true) . '</pre>';

From this image attached, how do I get or print the value of the [list] => Array, [board] => Array and [card] => Array?

json decoded

Please help me :(

jhovyn
  • 265
  • 3
  • 5
  • 17

4 Answers4

1

Looks like that the api return a array of arrays, then use foreach to iterate through the json_decode.

foreach ($json as $ticket) {
    // if your $ticket['data'] is a StdClass, force to be array
    $ticket['data'] = (array)$ticket['data'];

    // now you can access your indexes
    $list = $ticket['data']['list'];
    $board = $ticket['data']['board'];
    $card = $ticket['data']['card'];
}

If you don't like your $ticket['data'] as array, use as object instead $ticket['data']->list, $ticket['data']->board...

Thiago França
  • 1,817
  • 1
  • 15
  • 20
  • Thank you for your great time..I followed your code but i got this error message **Illegal string offset 'id'**,**Illegal string offset 'name'**,**Illegal string offset 'list'**,**Illegal string offset 'board'** and **"Cannot use object of type stdClass as array"** – jhovyn Mar 30 '17 at 06:16
  • `$ticket['data']` isn't an array, then you can use `$ticket['data']->list` or force `$ticket['data']` to be array like as `$data = (array) $ticket['data']` – Thiago França Mar 30 '17 at 18:27
1

After json_decode you can this way retrieve 'list','board' and 'card' array data....May be help you..

$list=array();
$board=array();
$card=array();

foreach($json as $key => $value){

   $list[]=$value[$key]['data']['list'];
   $board[]=$value[$key]['data']['board'];
   $card[]=$value[$key]['data']['card'];    
}

echo "<pre>";
print_r($list);
print_r($board);
print_r($card);
Ankur Radadiya
  • 245
  • 3
  • 11
  • Thank you for your great time..I followed your code but i got this error message **Illegal string offset 'id'**,**Illegal string offset 'name'**,**Illegal string offset 'list'**,**Illegal string offset 'board'** – jhovyn Mar 30 '17 at 06:05
  • I got this error also "Cannot use object of type stdClass as array" – jhovyn Mar 30 '17 at 06:12
0

You can access list array like below:

$list = $json['data']['list']

So access board array:

$board = $json['data']['board']

To access card array:

$card = $json['data']['card']

Imran
  • 4,582
  • 2
  • 18
  • 37
0

You get access for only 173 locaions array:

$list = $json[173]['data']['list'];
$board = $json[173]['data']['board'];
$card = $json[173]['data']['card'];

print_r($list);
print_r($board);
print_r($card);

you can get all of the array like

foreach ($json as $key => $value) { 
    $list[] = $value['data']['list'];
    $board[] = $value['data']['board'];
    $card[] = $value['data']['card'];
}

print_r($list);
print_r($board);
print_r($card);
tarikul05
  • 1,843
  • 1
  • 16
  • 24
  • Thank you for your great time..I followed your code but i got this error message **Illegal string offset 'id'**,**Illegal string offset 'name'**,**Illegal string offset 'list'**,**Illegal string offset 'board'** and **"Cannot use object of type stdClass as array"** – jhovyn Mar 30 '17 at 06:17
  • @jhovyn do not forget to add your `$str = file_get_contents('../trelloApi.json');` and `$json = json_decode($str, true);` lins – tarikul05 Mar 30 '17 at 06:37
  • Yes sir I did that..see my code '' – jhovyn Mar 30 '17 at 06:47
  • try second part of my answare @jhovyn – tarikul05 Mar 30 '17 at 06:51
  • I got this error Sir **Illegal string offset data,card,list,board**,**Undefined index: data**,**Array ( [0] => 5 [1] => S [2] => [3] => [4] => [5] => 5 [6] => [7] => [8] => [9] => h.......** – jhovyn Mar 30 '17 at 07:04