我试图用PHP
修改多维数组中的键和值[0] => Array (
[price] => 1-0
[shipping] => 0-1
[description] => 0-1
[meta_title] => 0-1
)
[1] => Array (
[price] => 1-0
[shipping] => 0-1
[meta_title] => 0-1
)
我想说我想在0中修改description
值和键名,我想用索引号做某些原因,到目前为止我的代码和我被困的地方:
$pageID = 0;
$text_id = 2;
$i=0;
foreach($oJson[$pageID] as $t => $f){
if($i==$text_id){
}
$i++;
}
期望的结果:
[0] => Array (
[price] => 1-0
[shipping] => 0-1
[modified_description] => 2-3
[meta_title] => 0-1
)
[1] => Array (
[price] => 1-0
[shipping] => 0-1
[meta_title] => 0-1
)
非常感谢你的宝贵帮助。
答案 0 :(得分:1)
答案 1 :(得分:1)
由于您希望保留关联键顺序,因此可以使用array_keys,array_values和array_combine。
$pageID = 0;
$text_id = 2;
$newKeys = array_keys($oJson[$pageID]);
$newValues = array_values($oJson[$pageID]);
$newKeys[$text_id] = 'modified_' . $newKeys[$text_id];
$newValues[$text_id] = '2-3';
$oJson[$pageID] = array_combine($newKeys, $newValues);