David
David

Reputation: 3

How to access multidimensional data from a json file?

I fetch students from $content= file_get_contents( '.../student.json')

$content= file_get_contents( '.../student.json');
$info = json_encode($content,true);

print_r($info);

when i try print_r($info) I have array like this:

 Array 
   (   [ID] => 1575 
[Header] => Array 

        ( 
[CreateDate] => 2017-04-01T05:41:46.2653231+02:00 
[CreateUser] => [Country] => 105
[CountryName] => Array ( [Caption] => Country [Value] => UK ) 
[CountryCode] => Array ( [Caption] => [Value] => )
[Nationality] => [NationalityNo] => 23509 [StudentNo] => 1001 
[ConfirmStatus] => Confirmed 
[Language] => EN [TotalArea] => 100
        )

   )

How to view all student's value?

Upvotes: 0

Views: 49

Answers (1)

Jimmix
Jimmix

Reputation: 6506

don't use encoding on already encoded data, but decode it and print in a loop by usnig foreach

$content = file_get_contents( '.../student.json');
$info = json_decode($content,true); //<== note decode not encode

foreach ($info as $key => $value) {
    echo '<hr>' . PHP_EOL;
    echo 'KEY: ' . $key . '<br>' . PHP_EOL;
    print_r($value);
}

you may also use var_dump instead of print_f here you have one array that has two arrays inside [1,2] and ["a","b"]:

$test = [[1,2],["a","b"]];
var_dump($test);

that outputs

array(2) {
  [0] =>
  array(2) {
    [0] =>
    int(1)
    [1] =>
    int(2)
  }
  [1] =>
  array(2) {
    [0] =>
    string(1) "a"
    [1] =>
    string(1) "b"
  }
}

Upvotes: 1

Related Questions