Zain Sohail
Zain Sohail

Reputation: 464

Push array in another multidimensional array

I am trying to add values in a multidimensional array. Given below is how its supposed to be

array(
    'name'  => 'Hotel',
    'placeholder'     => 'Select the hotel',
    'id'    => $prefix . 'confirmation_hotel',
    'type'  => 'select_advanced',
    'multiple'        => false,
    'options' => array(
        '5896' => 'Hotel 1',
        '6005' => 'Hotel 2'
    )
),

But I getting data of options from a custom function with a foreach loop, given below is the code.

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 output:

array(2) {
  ["name"]=>
  string(17) "Hotel 1"
  ["id"]=>
  string(4) "5896"
}
array(2) {
  ["name"]=>
  string(26) "Hotel 2"
  ["id"]=>
  string(4) "6005"
}

Now I need to add this data in the array given above. I'm not sure how to achieve this, can someone help me.

Upvotes: 0

Views: 41

Answers (2)

xander
xander

Reputation: 1765

I assume your first array at the top is called $a.

So you can append an array element to your 'options' sub array like this:

foreach ($order_names as $order_name) {
  array_push($a['options'], array($order_name['id'] => $order_name['name']));
}

Upvotes: 0

Romain Ciaccafava
Romain Ciaccafava

Reputation: 114

In the loop, after $order_names assignment, add :

$originalArray['options'][$order_names['id']] = $order_names['name'];

Upvotes: 2

Related Questions