开发人员正在向我发送一个多维JSON数组,它有一个我似乎无法从中获取信息的元素/数组。我附上了我的代码和解码数组的片段。
foreach($item['author'] as $sub){
if (is_array($sub)){
foreach($sub as $field => $value){
if ($field == "name"){
$author = $value;
} elseif ($field == "Request"){
$request = $value;
} elseif ($field == "Phone"){
$phone = $value;
....
数组是:
Array
(
[0] => Array
(
[field] => name
[value] => John Smith
)
[1] => Array
(
[field] => Request
[value] => order
)
[2] => Array
(
[field] => Phone
[value] => 123-555-1212
)
....
我的$ author,$ request和$ phone即将出现。
感谢您提供任何帮助。
吉姆
答案 0 :(得分:2)
字段是一个键,您想检查其中的值
foreach($item['author'] as $sub){
if (is_array($sub)){
foreach($sub as $field => $value){
if ($sub[$field] == "name"){
$author = $sub[value];
} elseif ($sub["$field"] == "Request"){
$request = $sub[value];
} elseif ($sub[$field] == "Phone"){
$phone = $sub[$value];
`
答案 1 :(得分:1)
您应该使用$value
而不是$field
。
$field
将是0,1,2(INDEX)等等..
所以解决方案将是:
foreach($a as $field => $value){
if ($value['field'] == "name"){
$author = $value['value'];
}
}