0

It seems to be a common question but I couldn't find any help...

I need to merge 4 arrays with different keys and values. So there are my 4 different arrays :

array(7) {
  ["id"]=>
  string(2) "32"
  ["title"]=>
  string(7) "Blettes"
  ["product_type_id"]=>
  string(2) "43"
  ["quantity"]=>
  string(4) "1.00"
  ["price"]=>
  string(4) "2.80"
  ["created_at"]=>
  string(19) "2011-09-03 11:31:35"
  ["proposition_vente"]=>
  string(1) "4"
}

array(4) {
  ["id"]=>
  string(2) "32"
  ["achat"]=>
  string(2) "47"
  ["total_price"]=>
  string(18) "131.59999999999994"
  ["total_vat"]=>
  string(18) "6.8619999999999965"
}

array(2) {
  ["id"]=>
  string(2) "32"
  ["exposition"]=>
  string(2) "46"
}

array(3) {
  ["id"]=>
  string(2) "32"
  ["sale_queue_id"]=>
  string(3) "163"
  ["exposition"]=>
  string(2) "56"
}

Into this one:

array(7) {
  ["id"]=>
  string(2) "32"
  ["title"]=>
  string(7) "Blettes"
  ["product_type_id"]=>
  string(2) "43"
  ["quantity"]=>
  string(4) "1.00"
  ["price"]=>
  string(4) "2.80"
  ["created_at"]=>
  string(19) "2011-09-03 11:31:35"
  ["proposition_vente"]=>
  string(1) "4"
  ["achat"]=>
  string(2) "47"
  ["total_price"]=>
  string(18) "131.5"
  ["total_vat"]=>
  string(18) "6.86"
  ["exposition"]=>
  string(2) "46"
  ["sale_queue_id"]=>
  string(3) "163"
  ["exposition"]=>
  string(2) "56"
}

I know I have to use the id to correctly merge them. I tried to use a combination of foreach() and array_merge() without success.

Any help is welcome :)

5
  • You have two exposition keys in your final array. Commented Jun 26, 2012 at 15:01
  • want to merge into single one? right? Commented Jun 26, 2012 at 15:02
  • ah yes I have to change some keys.. And yes I want to merge into single one Commented Jun 26, 2012 at 15:08
  • Can you show us what you've got so far? Commented Jun 26, 2012 at 15:09
  • I have four different functions, each return an array. Then I have: foreach($totalProposition as $key => $value) { $result[$key] = array_merge($value, $totalAchat[$key], $uniqueSale2, $exposition2 ); } Commented Jun 26, 2012 at 15:28

2 Answers 2

1
$result = array_merge($arr1, $arr2, $arr3, $arr4) should work.

You can try this too:

$result = array();
$source = array($arr1, $arr2, $arr3, $arr4);

foreach ($source as $a) {
    $result = array_merge($result, $a);
}
Sign up to request clarification or add additional context in comments.

1 Comment

the foreach + array_merge didn't work, also I have 4 main arrays with 20.000 different arrays inside. Like this [0]=>{ ["id"]=>18 }
0

I figure it out with this:

foreach ($totalProposition as $key => $value) {
$test[$key]=array_merge($totalProposition[$key], $totalAchat[$key], $uniqueSale2[$key], $exposition2[$key]);    
}

Thank you for your help : )

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.