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']
Justecho$array['fields'][19]['othername']
谢谢您的帮助谢谢您的帮助