У меня есть один массив под названием $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
)
Я хочу, чтобы ключ [Единицы] был изменен на [size_id] для каждого внутреннего массива. Какие изменения мне нужно внести в эту текущую функцию, которую я написал для достижения этой цели? Заранее спасибо.
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;
}
использовать вывод функции в качестве рабочего массива
Других решений пока нет …