-1

I tried and was able to print out the whole JSON file as string using this piece of code:

$str_data = file_get_contents("DB/TEITC502_OS.json");
$data = json_decode($str_data,true);

echo $str_data[0];

The JSON file with the following structure:

[
  {
    "id": 1,
    "name": "Overview of Operating system",
    "hours": 4,
    "sm": [
      {
        "smid": 1.1,
        "smn": "Overview of Operating system objectives and functions"
      },
      {
        "smid": 1.2,
        "smn": "Evolution of OS"
      }
     ]
  },
  {
    "id": 2,
    "name": "Process Management",
    "hours": 10,
    "sm": [
      {
        "smid": 2.1,
        "smn": "Process"
      },
      {
        "smid": 2.2,
        "smn": "Process States"
      },
      {
        "smid": 2.3,
        "smn": "Process Control Block (PCB)"
      }
     ]
   }
]

However I want to print out each of these details(id,name,hours,smid,smn) individually using PHP.

What changes should I make to the echo statement in-order to accomplish this?

2
  • you need to loop through the intire array. I will suggest to use a foreach() loop. Commented Mar 19, 2015 at 8:02
  • without the extra brackets and semicolons(since right now it is being printed as string), I am sorry it wan't clear. @SchalkKeun, I figured the same but I am unsure how to select the various nested array elements. Could you please provide some example or reference? Commented Mar 19, 2015 at 8:09

2 Answers 2

2

To echo in every row the keys and values, you can do it recursively:

function echo_array($a, $key){
    foreach($a as $key1 => $array1){
        if(!is_array($array1)){
            echo "$key $key1 : $array1<br/>";
        }else{
            echo_array($array1,"$key $key1");
        }
    }
}
echo_array($data,'');

Yields:

0 id : 1
0 name : Overview of Operating system
0 hours : 4
0 sm 0 smid : 1.1
0 sm 0 smn : Overview of Operating system objectives and functions
0 sm 1 smid : 1.2
0 sm 1 smn : Evolution of OS
1 id : 2
1 name : Process Management
1 hours : 10
1 sm 0 smid : 2.1
1 sm 0 smn : Process
1 sm 1 smid : 2.2
1 sm 1 smn : Process States
1 sm 2 smid : 2.3
1 sm 2 smn : Process Control Block (PCB)
Sign up to request clarification or add additional context in comments.

3 Comments

hey is there a way to make a nested list out of this?
for sure, try it, now I cannot because Im busy and only with phone, but tomorrow morning I can try, but it should be easy.
oops, so maybe its not that simple? Search on SO, for example stackoverflow.com/questions/12771708/…
2

you can use a loop to echo the array contents. I added a checking if the current value is an array so we can also get its inner contents.

foreach ($data as $dat) {
    if (is_array($dat)) {
        foreach ($dat as $d) {
            echo $d . '<br>';
        }
    } else {
        echo $dat . '<br> ' ;
    }
}

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.