-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?

Ashesh
  • 3,499
  • 1
  • 27
  • 44
  • you need to loop through the intire array. I will suggest to use a `foreach()` loop. – Schalk Keun Mar 19 '15 at 08: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? – Ashesh Mar 19 '15 at 08:09

2 Answers2

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.

Nicole
  • 109
  • 1
  • 9
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)
n-dru
  • 9,285
  • 2
  • 29
  • 42
  • hey is there a way to make a nested list out of this? – Ashesh Mar 19 '15 at 18:06
  • 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. – n-dru Mar 19 '15 at 18:17
  • 1
    oops, so maybe its not that simple? Search on SO, for example http://stackoverflow.com/questions/12771708/create-nested-list-from-multidimensional-array – n-dru Mar 19 '15 at 18:25