我有一個(gè)二維數(shù)組,我試圖在外部數(shù)組上循環(huán),$jsonarray并'color'根據(jù)開關(guān)的輸出向內(nèi)部數(shù)組添加一個(gè)鍵值對(duì)。當(dāng)我以 JSON 格式顯示輸出時(shí),'color'字段全部設(shè)置為 null。其$jsonarray結(jié)構(gòu)如下:$jsonarray= [ 0=> [ 'deadline' => 'deadline', 'duedate' => 'duedate',], 1=> [ 'deadline' => 'deadline', 'duedate' => 'duedate',],我的代碼如下所示: $i = 0; $keys = array_keys($jsonarray); for ($k = 0; $k < count($jsonarray); $k++){ foreach ($jsonarray[$keys[$k]] as $key => $value) { $color = 0; switch ($i) { case 0: $color = '#AED5F1'; break; case 1: $color = '#F7D590'; break; case 2: $color = '#EE8970'; break; } array_push($jsonarray[$k]['color'], $color); $i++; } }我怎樣才能解決這個(gè)問題?
1 回答

PIPIONE
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
嘗試這個(gè):
foreach ($jsonarray as $i => &$value) {
switch ($i) {
case 0:
$value['color'] = '#AED5F1';
break;
case 1:
$value['color'] = '#F7D590';
break;
case 2:
$value['color'] = '#EE8970';
break;
}
}
- 1 回答
- 0 關(guān)注
- 110 瀏覽
添加回答
舉報(bào)
0/150
提交
取消