1

Consider the below multi dimension array:

Array
(
[submit] => yes
[id] => 
[booking_id] => 
[booking_type_id] => Array
    (
        [0] => 171
        [1] => 58
    )

[value] => Array
    (
        [0] => 23
        [1] => 46
    )

)

How do I combine it so that that the booking_type_id and value arrays are in one array with the same values:

Array
(
    [new_values] => Array
        (
            [171] => 23
            [58] => 46
        )

)

I have tried array_merge and array_combine, but I can't get it to keep the keys? I have also tried to loop through and add to a new array.

2
  • Please show the code you have tried already -- the loop through/add to new array should work; there may just be a bug in your implementation. Commented Oct 10, 2014 at 9:45
  • Can you show the expected Array structure ? Commented Oct 10, 2014 at 9:46

5 Answers 5

3

How did you use array_combine. That should work for the structure you want. Example below:

$multi = array(
    'submit'          => 'yes',
    'id'              => '',    
    'booking_id'      => '', 
    'booking_type_id' => array( 171, 58 ),
    'value'           => array( 23, 46 ),
);

$combined = array_combine( $multi['booking_type_id'], $multi['value'] );
Sign up to request clarification or add additional context in comments.

1 Comment

Pleased I could help! :)
2

you can use array_combine() function like this:

$array['new_values'] = array_combine($array['booking_type_id'], $array['new_values']);

1 Comment

probably the most easy way to accomplish this. You just have to be sure that the number of elements is the same in each array.
1

A simple solution would be to loop through the booking_type_id array and correlate those values in a new array with:

$array_1['booking_type_id'] = array(171,58);
$array_1['value'] = array(23,46);

$array_2 = array(); // new combined array

foreach ($array_1['booking_type_id'] as $key => $value) {
    $array_2[$value] = $array_1['value'][$key];
}

With the result being:

Array
(
    [171] => 23
    [58] => 46
)

UPDATE:

As others have already noted you can also accomplish the same with array_combine()

$array_2 = array_combine( $array_1['booking_type_id'], $array_1['value'] );

Comments

0
<?php
    $i = 0;
    $new_values = array();
    while($i < count($your_array['booking_type_id']))
    {
        $new_values['new_values'][$your_array['booking_type_id'][$i]] = 
$your_array['value'];
        $i++;
    }
?>

Comments

0
$arr = array(
        'submit' => "yes",
        'id' => NULL,
        'booking_id' => NULL,
        'booking_type_id' => array(
                0 => 171,
                1 => 58
        ),  
        'value' => array(
                0 => 23,
                1 => 46
        )

);


$new_arr = array();
foreach($arr['booking_type_id'] as $key=>$value){

    $new_arr[$value] = $arr['value'][$key];

}
$arr['new_values'] =  $new_arr;

echo"<pre>";print_r($arr);

Result will be

Array
(
[submit] => yes
[id] => 
[booking_id] => 
[booking_type_id] => Array
    (
        [0] => 171
        [1] => 58
    )

[value] => Array
    (
        [0] => 23
        [1] => 46
    )

[new_values] => Array
    (
        [171] => 23
        [58] => 46
    )

)

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.