Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-数组中的数组_Php_Arrays - Fatal编程技术网

PHP-数组中的数组

PHP-数组中的数组,php,arrays,Php,Arrays,我有一个如下所示的数组: Array ( [id] => 01 [name] => johndoe [fields] => Array ( [19] => Array ( [othername] => fieldname ) ) ) 如何从othername获取值 我

我有一个如下所示的数组:

Array
(
    [id] => 01
    [name] => johndoe   
    [fields] => Array
        (
             [19] => Array
                 (
                      [othername] => fieldname
                 )
        )
)
如何从othername获取值

我试过:

 $array = array();
 $array[fields] = array();
 echo $array[19]['othername'] ;
但我想我需要再上一层楼

$array = array();         //-| 
                          // | This resets the variable to blank array. Remove it.
$array[fields] = array(); //-|

// assuming that variable dumped is $array, directly use this.
echo $array['fields'][19]['othername'];
但是,我建议,您不应该使用这种修复语法获取所需的值。我的回答只是为了解决你手头的问题。实现编码最佳实践超出了这个答案的范围。您应该尝试递归地循环并获取所需的值

$data = array(
    'id' => '01',
    'name' => 'johndoe',
    'fields' => array(
        array(
            'othername' => "fieldname"
        )
    ),
);
//Inside array is array of array so foreach() is used
foreach ($data['fields'] as $row)
{
  echo $row['othername'];
}
但是,我建议,您不应该使用这种修复语法获取所需的值。我的回答只是为了解决你手头的问题。实现编码最佳实践超出了这个答案的范围。您应该尝试递归地循环并获取所需的值。

只需简单地使用

$data = array(
    'id' => '01',
    'name' => 'johndoe',
    'fields' => array(
        array(
            'othername' => "fieldname"
        )
    ),
);
//Inside array is array of array so foreach() is used
foreach ($data['fields'] as $row)
{
  echo $row['othername'];
}
$array['fields'][19]['othername']
只需简单地使用

$array['fields'][19]['othername']

Just
echo$array['fields'][19]['othername']
Just
echo$array['fields'][19]['othername']
谢谢您的帮助谢谢您的帮助