首先让我说我在这里经历了很多相关的问题,但没有一个能解决我的问题。我有和db嵌套数组在db中,需要打印所有的值。输出打印出第一个最顶层的数组('名称'和值" Office,home")但没有得到包含在&#39中的嵌套数组值; BB&#34 ;. 这是我的代码:
function getTB($name='', $value='') {
$value = json_decode($value);
$name = ''; $BB= '';
foreach ($value as $key=>$val) {
$name .= $val['name'].',';
JSON代码:
[{"name":"Office","TB":[{"date":[false,true],"time":"8:00 AM"}]},{{"days":[false,true],"time":"6:00 PM"},
答案 0 :(得分:1)
使用array_walk_recursive
例如,如果你有像这样的json(有一个子阵列)
$decoded = json_decode('{"abc": "one",
"def": "two",
"sub_array": {
"ABC": "ONE"
}}', true);
array_walk_recursive($decoded, function($value,$key) {
echo $key.' :'.$value.'<br>';
});
输出将是
abc :one
def :two
ABC :ONE
所以像这样使用而不是普通的foreach
<强> 更新 强>
像这样使用
$arr='[{"name":"Office","TB":[{"date":[false,true],"time":"8:00 AM"},{"days":[false,true],"time":"6:00 PM"}]}]';
$rr=json_decode($arr);
foreach ($rr[0]->TB as $key => $value) {
array_walk_recursive($value, function($k,$kk){
echo 'Key is '.$kk.' & Value is '.$k.'<br>';
});
}
<强> 输出: 强>
Key is 0 & Value is
Key is 1 & Value is 1
Key is time & Value is 8:00 AM
Key is 0 & Value is
Key is 1 & Value is 1
Key is time & Value is 6:00 PM