0

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.

2
  • I'll change that. The resulting data is in there, the first piece of code is how its supposed to be Commented Nov 9, 2017 at 14:05
  • Yea, just edited the question Commented Nov 9, 2017 at 14:07

2 Answers 2

2

In the loop, after $order_names assignment, add :

$originalArray['options'][$order_names['id']] = $order_names['name'];
Sign up to request clarification or add additional context in comments.

Comments

0

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']));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.