2

I have a case to create or merge two arrray into single array.

First array like this :

Array // First Array
(
    [0] => Array
        (
            [hotel] => cempaka
            [vendor] => mas
        )

    [1] => Array
        (
            [hotel] => permata
            [vendor] => sip
        )
)

Second array :

Array
(
    [0] => Array
    (
        [order_type] => 1
        [currency] => 26
        [net] => 26000
        [rate] => 26000
        [amount] => 26000
        [bank_surcharge] => 26000
        [ticket] => 26000
        [profit] => 26000
        [selling_price] => 26000
        [description] => a
    )

[1] => Array
    (
        [order_type] => 2
        [currency] => 27
        [net] => 27000
        [rate] => 27000
        [amount] => 27000
        [bank_surcharge] => 27000
        [ticket] => 27000
        [profit] => 27000
        [selling_price] => 27000
        [description] => b
    )

[2] => Array
    (
        [order_type] => 5
        [currency] => 28
        [net] => 28000
        [rate] => 28000
        [amount] => 28000
        [bank_surcharge] => 28000
        [ticket] => 28000
        [profit] => 28000
        [selling_price] => 28000
        [description] => c
    )

[3] => Array
    (
        [order_type] => 3
        [currency] => 29
        [net] => 29000
        [rate] => 29000
        [amount] => 29000
        [bank_surcharge] => 29000
        [ticket] => 29000
        [profit] => 29000
        [selling_price] => 29000
        [description] => d
    )

[4] => Array
    (
        [order_type] => 4
        [currency] => 30
        [net] => 30000
        [rate] => 30000
        [amount] => 30000
        [bank_surcharge] => 30000
        [ticket] => 30000
        [profit] => 30000
        [selling_price] => 30000
        [description] => e
    )

[5] => Array
    (
        [order_type] => 6
        [currency] => 31
        [net] => 31000
        [rate] => 31000
        [amount] => 31000
        [bank_surcharge] => 31000
        [ticket] => 31000
        [profit] => 31000
        [selling_price] => 31000
        [description] => f
    )

[6] => Array
    (
        [order_type] => 1
        [currency] => 32
        [net] => 32000
        [rate] => 32000
        [amount] => 32000
        [bank_surcharge] => 32000
        [ticket] => 32000
        [profit] => 32000
        [selling_price] => 32000
        [description] => g
    )

[7] => Array
    (
        [order_type] => 2
        [currency] => 33
        [net] => 33000
        [rate] => 33000
        [amount] => 33000
        [bank_surcharge] => 33000
        [ticket] => 33000
        [profit] => 33000
        [selling_price] => 33000
        [description] => h
    )

[8] => Array
    (
        [order_type] => 5
        [currency] => 34
        [net] => 34000
        [rate] => 34000
        [amount] => 34000
        [bank_surcharge] => 34000
        [ticket] => 34000
        [profit] => 34000
        [selling_price] => 34000
        [description] => i
    )

[9] => Array
    (
        [order_type] => 3
        [currency] => 35
        [net] => 35000
        [rate] => 35000
        [amount] => 35000
        [bank_surcharge] => 35000
        [ticket] => 35000
        [profit] => 35000
        [selling_price] => 35000
        [description] => j
    )

[10] => Array
    (
        [order_type] => 4
        [currency] => 36
        [net] => 36000
        [rate] => 36000
        [amount] => 36000
        [bank_surcharge] => 36000
        [ticket] => 36000
        [profit] => 36000
        [selling_price] => 36000
        [description] => k
    )

[11] => Array
    (
        [order_type] => 6
        [currency] => 37
        [net] => 37000
        [rate] => 37000
        [amount] => 37000
        [bank_surcharge] => 37000
        [ticket] => 37000
        [profit] => 37000
        [selling_price] => 37000
        [description] => l
    )
)

and then this the output that i want to get:

Array
(
[0] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 1
        [currency] => 26
        [net] => 26000
        [rate] => 26000
        [amount] => 26000
        [bank_surcharge] => 26000
        [ticket] => 26000
        [profit] => 26000
        [selling_price] => 26000
        [description] => a
    )

[1] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 2
        [currency] => 27
        [net] => 27000
        [rate] => 27000
        [amount] => 27000
        [bank_surcharge] => 27000
        [ticket] => 27000
        [profit] => 27000
        [selling_price] => 27000
        [description] => b
    )

[2] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 5
        [currency] => 28
        [net] => 28000
        [rate] => 28000
        [amount] => 28000
        [bank_surcharge] => 28000
        [ticket] => 28000
        [profit] => 28000
        [selling_price] => 28000
        [description] => c
    )

[3] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 3
        [currency] => 29
        [net] => 29000
        [rate] => 29000
        [amount] => 29000
        [bank_surcharge] => 29000
        [ticket] => 29000
        [profit] => 29000
        [selling_price] => 29000
        [description] => d
    )

[4] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 4
        [currency] => 30
        [net] => 30000
        [rate] => 30000
        [amount] => 30000
        [bank_surcharge] => 30000
        [ticket] => 30000
        [profit] => 30000
        [selling_price] => 30000
        [description] => e
    )

[5] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 6
        [currency] => 31
        [net] => 31000
        [rate] => 31000
        [amount] => 31000
        [bank_surcharge] => 31000
        [ticket] => 31000
        [profit] => 31000
        [selling_price] => 31000
        [description] => f
    )

[6] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 1
        [currency] => 32
        [net] => 32000
        [rate] => 32000
        [amount] => 32000
        [bank_surcharge] => 32000
        [ticket] => 32000
        [profit] => 32000
        [selling_price] => 32000
        [description] => g
    )

[7] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 2
        [currency] => 33
        [net] => 33000
        [rate] => 33000
        [amount] => 33000
        [bank_surcharge] => 33000
        [ticket] => 33000
        [profit] => 33000
        [selling_price] => 33000
        [description] => h
    )

[8] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 5
        [currency] => 34
        [net] => 34000
        [rate] => 34000
        [amount] => 34000
        [bank_surcharge] => 34000
        [ticket] => 34000
        [profit] => 34000
        [selling_price] => 34000
        [description] => i
    )

[9] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 3
        [currency] => 35
        [net] => 35000
        [rate] => 35000
        [amount] => 35000
        [bank_surcharge] => 35000
        [ticket] => 35000
        [profit] => 35000
        [selling_price] => 35000
        [description] => j
    )

[10] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 4
        [currency] => 36
        [net] => 36000
        [rate] => 36000
        [amount] => 36000
        [bank_surcharge] => 36000
        [ticket] => 36000
        [profit] => 36000
        [selling_price] => 36000
        [description] => k
    )

[11] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 6
        [currency] => 37
        [net] => 37000
        [rate] => 37000
        [amount] => 37000
        [bank_surcharge] => 37000
        [ticket] => 37000
        [profit] => 37000
        [selling_price] => 37000
        [description] => l
    )
)

(This two array i get from dynamic field generated via javascript) Any solution will be appreciated a lot.. Thanks

1
  • it doesnt work to give me output i wanted. So, basically, i have multiple field form by javascript & html, and i want pass its value to array with php. So i can show it on screen or insert it to db. let's say first array is heads and second array is items. If heads[0] then it will be merge with items[0] until items[5]. and if heads[1] then it will be merge with items[6] until items[11]. and if heads[3], it will be merge with items[12] until items[17] and so on.. any help sir? i want it dynamic sir.. Commented Oct 31, 2013 at 6:39

3 Answers 3

1

Use the following since you are trying to merge arrays based on the key number of the second array:

foreach($second_array as $key => $value){
    if($key >= 0 && $key <= 5){
        $new_array[$key] = array_merge($second_array[$key], $first_array[0]);
    }
    if($key >= 6 && $key <= 11){
        $new_array[$key] = array_merge($second_array[$key], $first_array[1]);
    }
    //etc.......
}
Sign up to request clarification or add additional context in comments.

3 Comments

like i said at the bottom post, that's two array i got from dynamic multi field (i dont show full my code, cos i scare it will be take a long word). So, basically, i have multiple field form by javascript & html, and i want pass its value to array with php. So i can show it on screen or insert it to db. let's say first array is heads and second array is items. If heads[0] then it will be merge with items[0] until items[5]. and if heads[1] then it will be merge with items[6] until items[11]. and if heads[3], it will be merge with items[12] until items[17] and so on.. any help sir?
code above meet my needs sir :) but how to make it dynamic fo looping conditional $key for (// etc.... i need it dynamic) any idea, solution sir?
@ Lloyd Banks i'm finnaly can make it dynamic sir, thanks for your solution
1

Try like

$arr_cnt = count($arr1);
for($i = 0 ; $i < $arr_cnt ; $i++) {

    foreach($arr1[$i] as $key1 => $value1) {    
        $new_arr[$i][$key1] = $value1;
    }
    foreach($arr2[$i] as $key2 => $value2) {    
        $new_arr[$i][$key2] = $value2;
    }
}
print_r($new_arr);

1 Comment

it doesnt work to give me output i wanted. So, basically, i have multiple field form by javascript & html, and i want pass its value to array with php. So i can show it on screen or insert it to db. let's say first array is heads and second array is items. If heads[0] then it will be merge with items[0] until items[5]. and if heads[1] then it will be merge with items[6] until items[11]. and if heads[3], it will be merge with items[12] until items[17] and so on.. any help sir?
1
 $outputArray = array();
 foreach ($firstArray as $location) {
     foreach ($secondArray as $option) {
         $outputArray[] = array_merge($location,$option);
     }
 }

1 Comment

i try code above but doesnt work to what i need.. So, basically, i have multiple field form by javascript & html, and i want pass its value to array with php. So i can show it on screen or insert it to db. let's say first array is heads and second array is items. If heads[0] then it will be merge with items[0] until items[5]. and if heads[1] then it will be merge with items[6] until items[11]. and if heads[3], it will be merge with items[12] until items[17] and so on.. any help sir?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.