修改多维数组PHP中的键和值

时间:2016-05-23 10:48:03

标签: php arrays multidimensional-array

我试图用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
        )

非常感谢你的宝贵帮助。

2 个答案:

答案 0 :(得分:1)

试试这个:

{{1}}

工作示例:CLICK!

答案 1 :(得分:1)

由于您希望保留关联键顺序,因此可以使用array_keysarray_valuesarray_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);