[英]How to push an array in a multidimensional array into a specific subarray in another multidimensional array
[英]Push array in another multidimensional array
我正在尝试在多维数组中添加值。 下面给出的是应该如何
array(
'name' => 'Hotel',
'placeholder' => 'Select the hotel',
'id' => $prefix . 'confirmation_hotel',
'type' => 'select_advanced',
'multiple' => false,
'options' => array(
'5896' => 'Hotel 1',
'6005' => 'Hotel 2'
)
),
但是我从带有foreach循环的自定义函数中获取options
数据,下面是代码。
global $bookyourtravel_accommodation_helper, $bookyourtravel_car_rental_helper;
$items = $order->get_items();
$names = array();
foreach ( $items as $item_id => $item ) {
$bookyourtravel_theme_woocommerce = BookYourTravel_Theme_WooCommerce::get_instance();
$bookyourtravel_theme_woocommerce->init();
$order_names = $bookyourtravel_theme_woocommerce->order_item_name_confirmation($item);
}
$order_names
输出:
array(2) {
["name"]=>
string(17) "Hotel 1"
["id"]=>
string(4) "5896"
}
array(2) {
["name"]=>
string(26) "Hotel 2"
["id"]=>
string(4) "6005"
}
现在,我需要将此数据添加到上面给出的数组中。 我不确定如何实现这一目标,有人可以帮助我。
在循环中,在分配$order_names
之后,添加:
$originalArray['options'][$order_names['id']] = $order_names['name'];
我假设您在顶部的第一个数组称为$a
。
因此,您可以像这样将数组元素附加到“选项”子数组中:
foreach ($order_names as $order_name) {
array_push($a['options'], array($order_name['id'] => $order_name['name']));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.