Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  i alarmed alien 10 mins ago
    
Can you show the expected Array structure ? –  Wissam El-Kik 9 mins ago
    
Array ( [new_values] => Array ( [171] => 23 [58] => 46 ) ) –  Pete Naylor 8 mins ago

5 Answers 5

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

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
)
share

you can use array_combine()

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

share
    
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. –  jpicaude 30 secs ago
<?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++;
    }
?>
share
$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;

print_r($arr);
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.