0

I am trying to display JSON records. I have successfully gotten values for id, chart_id, first_name.

My issue is that values for office parameters throws error of undefined offset. How do I also print values for offices parameters?

Below is the code:

<?php

$output ='
{"previous":null,"results":[
{"id":91168488,"chart_id":"SMAM000001", "offices":[3033],"first_name":"Nancy"},
{"id":91168489,"chart_id":"MADE000004", "offices":[3044], "first_name":"Moore"}]
}
';

$json = json_decode($output, true);

foreach($json["results"] as $v1){
    echo $id = $v1['id'];
    echo "<br><br>";

    echo $chart_id = $v1['chart_id'];
    echo "<br><br>";

    echo $first_name = $v1['first_name'];
    echo "<br><br>";
    
    echo $offices = $v1['offices'];
    echo "<br><br>";
}
?>
5
  • Good code indentation would help us read the code and more importantly it will help you debug your code Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. Commented Dec 29, 2020 at 17:50
  • 2
    echo $offices = $v1['offices'][0]; Commented Dec 29, 2020 at 17:53
  • maybe $offices = implode(', ', $v1['offices']); would do it since it is an array. Commented Dec 29, 2020 at 17:54
  • The brackets around [3033] in "offices":[3033] makes that another array :) Commented Dec 29, 2020 at 17:55
  • Thanks everyone. special Thanks to you Stefan. your suggestions works for me. you can update it as the right answer. Commented Dec 29, 2020 at 17:56

1 Answer 1

2

Since [3033] is an array, just add [0] at the end of $v1['offices']

echo $offices = $v1['offices'][0];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.