如何在 PHP 中访问嵌套关联数组数据

2022-01-07 00:00:00 arrays nested parsing php associative

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

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.

相关文章