【发布时间】:2014-12-10 13:26:03
【问题描述】:
我有一个标题为$rebate_by_prod 的数组,我想修改它。数组内容如下:
Array
(
[op] => preview
[id] => 280
[form_submitted] => yes
[rebate_id] => 280
[main_op] => edit
[site_url] => http://localhost/smart-rebate-web/web/
[rebate_image_url] => http://localhost/smart-rebate-web/web/images/rebates/rebate_images_by_product/2014/rebate_1412690625.gif
[image_path] => rebates/rebate_images_by_product/2014/rebate_1412690625.gif
[brand_id] => 134
[1] => Array
(
[product_id_1] => Array
(
[0] => 214
[1] => 215
[2] => 216
)
[pack] => 1
[quantity] => 1
[units] => 51
[amount] => 3.0
)
[2] => Array
(
[pack] => 1
[quantity] => 2
[units] => 49
[amount] => 10.0
[product_id_2] => Array
(
[0] => 214
[1] => 215
[2] => 216
)
)
[title] => Three Olives Vodka
[rebate_start_date] =>
[rebate_expiry_date] => 2014-10-31
[applicable_states] => Array
(
[0] => 30
)
[multiselect] => 30
[rebate_total_count] => 150
[details] => Three Olives
)
现在我编写了一个函数,它在进行一些必要的修改后生成以下输出数组: 函数如下:
function change_product_keys($reb_by_product) {
foreach ($reb_by_product as $index => $sub) {
if (is_array($sub)) {
foreach ($sub as $key => $value) {
if (strpos($key, 'product_id_') !== FALSE) {
$reb_by_product[$index]['products'] = $value;
unset($reb_by_product[$index][$key]);
}
}
}
}
return $reb_by_product;
}
标题为$reb_by_product 的输出结果数组如下:
Array
(
[op] => preview
[id] => 280
[form_submitted] => yes
[rebate_id] => 280
[main_op] => edit
[site_url] => http://localhost/smart-rebate-web/web/
[rebate_image_url] => http://localhost/smart-rebate-web/web/images/rebates/rebate_images_by_product/2014/rebate_1412690625.gif
[image_path] => rebates/rebate_images_by_product/2014/rebate_1412690625.gif
[brand_id] => 134
[1] => Array
(
[pack] => 1
[quantity] => 1
[units] => 51
[amount] => 3.0
[products] => Array
(
[0] => 214
[1] => 215
[2] => 216
)
)
[2] => Array
(
[pack] => 1
[quantity] => 2
[units] => 49
[amount] => 10.0
[products] => Array
(
[0] => 214
[1] => 215
[2] => 216
)
)
[title] => Three Olives Vodka
[rebate_start_date] =>
[rebate_expiry_date] => 2014-10-31
[applicable_states] => Array
(
[0] => 30
)
[multiselect] => 30
[rebate_total_count] => 150
[details] => Three Olives
)
其实我想要的操作数组如下:
Array
(
[op] => preview
[id] => 280
[form_submitted] => yes
[rebate_id] => 280
[main_op] => edit
[site_url] => http://localhost/smart-rebate-web/web/
[rebate_image_url] => http://localhost/smart-rebate-web/web/images/rebates/rebate_images_by_product/2014/rebate_1412690625.gif
[image_path] => rebates/rebate_images_by_product/2014/rebate_1412690625.gif
[brand_id] => 134
[1] => Array
(
[pack] => 1
[quantity] => 1
[size_id] => 51
[amount] => 3.0
[products] => Array
(
[0] => 214
[1] => 215
[2] => 216
)
)
[2] => Array
(
[pack] => 1
[quantity] => 2
[size_id] => 49
[amount] => 10.0
[products] => Array
(
[0] => 214
[1] => 215
[2] => 216
)
)
[title] => Three Olives Vodka
[rebate_start_date] =>
[rebate_expiry_date] => 2014-10-31
[applicable_states] => Array
(
[0] => 30
)
[multiselect] => 30
[rebate_total_count] => 150
[details] => Three Olives
)
我希望将每个内部数组的 [units] 键更改为 [size_id]。我需要对我为实现这一目标而编写的当前函数进行哪些更改?提前致谢。
【问题讨论】:
-
如果您直接将该内部数组分配为整个数组,则需要使用键
size_id创建新元素,然后取消设置units,然后再将整个内部数组分配给外部数组.如果您正在逐个元素地创建该内部数组,那么您需要检查您要创建的当前元素的键是否为units,并使用size_id作为新生成元素的键。 -
@CBroe:如果您对我编写的函数代码进行更改并发布整个函数代码作为答案,这对我和其他人都会非常有用。
-
这不太可能对其他人有很大帮助,因为您的数组结构非常具体。而且我不会给你一个可以复制和粘贴的解决方案——你自己试试吧。
标签: php arrays multidimensional-array associative-array key-value