我想要修改一个标题为$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]。我为实现这个目的而编写的当前函数需要做些什么改变?提前谢谢。
答案 0 :(得分:0)
function change_product_keys($reb_by_product) {
$reb_by_product_copy = $reb_by_product;
foreach ($reb_by_product_copy 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]);
###############################
$reb_by_product[$index]['size_id'] = $reb_by_product[$index]['units'];
unset($reb_by_product[$index]['units']);
################################
}
}
}
}
return $reb_by_product;
}
使用函数输出作为工作数组