【问题标题】:Multi dimensional associative array in phpphp中的多维关联数组
【发布时间】:2018-07-17 02:18:05
【问题描述】:

我需要一些关于下面关联数组的指南,我如何访问或检索指定的值,例如 JSON 格式的 student_details 中的 student_name "Kevin" 下的高度。

<?php
$college=[
    'college_id' => '123',
    'student_details'=> [
        'student_name'=>[
            'Kevin'=> [
                'height'=>'170',
                'weight'=>'65',
            ],
            'Daniel'=> [
                'height'=>'170',
                'weight'=>'65',
            ] ,
            'Paul'=> [
                'height'=>'179',
                'weight'=>'70',
            ]
        ]
    ]
];
echo json_encode($college);
?>

【问题讨论】:

  • 你为什么要尝试在 PHP 中处理 JSON?您应该只操作数组并转换为 JSON 以进行输出。
  • 如果我需要将这个数组以 JSON 编码传递到另一个页面,我应该如何访问其中的值?
  • 另一个页面是 PHP 还是 JavaScript?如果是 PHP 那么你可以使用json_decode()
  • PHP,在我使用json解码后,我应该如何访问我上面提到的指定数据,例如student_details中student_name“Kevin”下的高度
  • 在下面查看我的答案

标签: php arrays json associative-array


【解决方案1】:

如果你使用$array = json_decode($json, true);

<?php
echo $array['student_details']['student_name']['Kevin']['height'];
?>

或者如果你使用$object = json_decode($json);

<?php
echo $object->student_details->student_name->Kevin->height;
?>

【讨论】:

  • 谢谢老哥,帮了大忙
猜你喜欢
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
相关资源
最近更新 更多