0

When i try to print specific items in the array "entry_history" , i get an error. For example when i am printing ID it gives me the following error:

"Warning: Illegal string offset 'id'"

<?PHP
foreach($data['entry_history'] as $key=>$item)
{ 

?>
<td><?PHP echo $item['id']; ?></td>
<?PHP
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Zain
  • 21
  • 5
  • `$item` is a string, not an array. – Barmar Aug 13 '18 at 18:40
  • @Barmar Good point :) – RiggsFolly Aug 13 '18 at 18:41
  • `$data['entry_history']` is a 1-dimensional array, not a 2-dimensional array. – Barmar Aug 13 '18 at 18:42
  • To ask for help it is a good idea to show the code and the data as well. Can you show us a `var_dump($data)` – RiggsFolly Aug 13 '18 at 18:42
  • Thank you guys or your response. I actually am editing a code from github. Here is the link to the code https://github.com/pkgrem/OddsOnFPL/blob/master/fplinfo.php . The error is on line 81. – Zain Aug 13 '18 at 18:52
  • @Barmar, if its a 1 dimensional array, how can i print specific values from the array ? – Zain Aug 13 '18 at 18:59
  • @RiggsFolly here you go, this is the data i am retrieving from the api https://fantasy.premierleague.com/drf/entry/1/event/1/picks – Zain Aug 13 '18 at 19:00
  • You want `$data['entry_history']['id']`. – Barmar Aug 13 '18 at 19:01
  • Or `$data->entry_history->id` if you didnt convert it all to an array in the `json_decode()` – RiggsFolly Aug 13 '18 at 19:02
  • @Zain Why wouldn't you be able to print specific values from a 1-dimensional array? But you can't treat the values as nested arrays, since they're scalar. – Barmar Aug 13 '18 at 19:04
  • @Barmar and RiggsFolly thank you so much! Can you please explain what i was doing wrong? Isn't 'entry_history' an array? – Zain Aug 13 '18 at 19:06
  • Yes, it's an array, but it's not a 2-dimensional array. `id` is a key in `entry_history`, but you looped through each element of the array, and tried to access the `id` key of the element. – Barmar Aug 13 '18 at 19:08
  • Your code would work for `"event_history": [ { "id" => ... }, {"id" => ...}, ...]` See the difference? – Barmar Aug 13 '18 at 19:09
  • Look at `picks` for an example of an array of objects. – Barmar Aug 13 '18 at 19:10
  • @Barmar i get what you mean now. However the same code is not working for picks. I just replaced 'entry_history' with 'picks' but its showing undefined index error. – Zain Aug 13 '18 at 20:04
  • There's no `id` in `picks`, use `element` instead. – Barmar Aug 13 '18 at 20:10
  • @Barmar yeah that was a stupid mistake, i figured it out instantly. Thanks for all the help! – Zain Aug 13 '18 at 21:04

0 Answers0