如何在数组中显示数组

时间:2018-10-07 00:57:57

标签: php arrays multidimensional-array

我从$ content = file_get_contents('... / student.json')获取学生

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

print_r($info);

当我尝试print_r($info)时,我的数组如下:

 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
        )

   )

如何查看所有学生的价值?

1 个答案:

答案 0 :(得分:1)

不要对已经编码的数据使用编码,而是将其解码并通过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);
}

您也可以使用var_dump而不是print_f,这里有一个数组,其中[1,2]和[“ a”,“ b”]内有两个数组:

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

输出

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