Welcome to the Cookbook

loading...

8.5.21 merge

array Set::merge ($arr1, $arr2=null)

This function can be thought of as a hybrid between PHP's array_merge and array_merge_recursive. The difference to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge) but does not do if for keys containing strings (unlike array_merge_recursive). See the unit test for more information.

This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.

$arry1 = array(
	array(
		'id' => '48c2570e-dfa8-4c32-a35e-0d71cbdd56cb',
		'name' => 'mysql raleigh-workshop-08 < 2008-09-05.sql ',
		'description' => 'Importing an sql dump'
	),
	array(
		'id' => '48c257a8-cf7c-4af2-ac2f-114ecbdd56cb',
		'name' => 'pbpaste | grep -i Unpaid | pbcopy',
		'description' => 'Remove all lines that say "Unpaid".',
	)
);
$arry2 = 4;
$arry3 = array(0=>"test array", "cats"=>"dogs", "people" => 1267);
$arry4 = array("cats"=>"felines", "dog"=>"angry");
$res = Set::merge($arry1, $arry2, $arry3, $arry4);

/* $res now looks like: 
Array
(
    [0] => Array
        (
            [id] => 48c2570e-dfa8-4c32-a35e-0d71cbdd56cb
            [name] => mysql raleigh-workshop-08 < 2008-09-05.sql 
            [description] => Importing an sql dump
        )

    [1] => Array
        (
            [id] => 48c257a8-cf7c-4af2-ac2f-114ecbdd56cb
            [name] => pbpaste | grep -i Unpaid | pbcopy
            [description] => Remove all lines that say "Unpaid".
        )

    [2] => 4
    [3] => test array
    [cats] => felines
    [people] => 1267
    [dog] => angry
)
*/
  1. $arry1 = array(
  2. array(
  3. 'id' => '48c2570e-dfa8-4c32-a35e-0d71cbdd56cb',
  4. 'name' => 'mysql raleigh-workshop-08 < 2008-09-05.sql ',
  5. 'description' => 'Importing an sql dump'
  6. ),
  7. array(
  8. 'id' => '48c257a8-cf7c-4af2-ac2f-114ecbdd56cb',
  9. 'name' => 'pbpaste | grep -i Unpaid | pbcopy',
  10. 'description' => 'Remove all lines that say "Unpaid".',
  11. )
  12. );
  13. $arry2 = 4;
  14. $arry3 = array(0=>"test array", "cats"=>"dogs", "people" => 1267);
  15. $arry4 = array("cats"=>"felines", "dog"=>"angry");
  16. $res = Set::merge($arry1, $arry2, $arry3, $arry4);
  17. /* $res now looks like:
  18. Array
  19. (
  20. [0] => Array
  21. (
  22. [id] => 48c2570e-dfa8-4c32-a35e-0d71cbdd56cb
  23. [name] => mysql raleigh-workshop-08 < 2008-09-05.sql
  24. [description] => Importing an sql dump
  25. )
  26. [1] => Array
  27. (
  28. [id] => 48c257a8-cf7c-4af2-ac2f-114ecbdd56cb
  29. [name] => pbpaste | grep -i Unpaid | pbcopy
  30. [description] => Remove all lines that say "Unpaid".
  31. )
  32. [2] => 4
  33. [3] => test array
  34. [cats] => felines
  35. [people] => 1267
  36. [dog] => angry
  37. )
  38. */