如何在 PHP 中访问嵌套关联数组数据 [英] How to access nested associative array data in PHP

查看:31
本文介绍了如何在 PHP 中访问嵌套关联数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个索引数组,其中包含一个嵌套关联数组和一个嵌套索引数组:

I have an indexed array that contains a nested associative array AND a nested indexed array:

$myArray = array ( 
    0 => array (
        'name' => 'Paul', 
        'age' => '23', 
        'hobbies' => array ( 
            0 => 'basketball',
        ), 
        'pets' => 'dog',
    ),
);

如何访问所有这些值并将它们转换为变量?

How can I access all of these values and convert them into variables?

推荐答案

你可以直接从 Array 访问

You can just access from Array

像这样写你的数组

$myArray = [
  0 => [
      'name' => 'Paul',
      'age' => '23',
      'hobbies' => [
              0 => 'basketball',
            ],
      'pets' => 'dog'
    ]
];

假设你想访问第一个元素的名字

Suppose you want to access name of first elements

echo $myArray[0]['name']; // it will print 'Paul'
echo $myArray[0]['hobbies'][0]; // it will print basketball

现在你可以像上面一样获取.

Now you can fetch like above.

这篇关于如何在 PHP 中访问嵌套关联数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆