array-merge

php array_merge_recursive preserving numeric keys

匆匆过客 提交于 2019-11-30 04:43:07
I would simply like to merge $a = array("59745506"=>array("up" => 0,)); $b = array("59745506"=>array("text" => "jfrj")); $c = array_merge_recursive_new($a, $b); result: Array ( [0] => Array ( [up] => 0 ) [1] => Array ( [text] => jfrj ) ) expected result: Array ( [59745506] => Array ( [up] => 0 [text] => jfrj ) ) the 2nd comment in http://www.php.net/manual/en/function.array-merge-recursive.php is working, is it the best solution for my case (where I need to merge arrays with multiple numeric keys, and with 2 levels)? another workaround would be to implement it with array_map(function ()... The

Array-Merge on an associative array in PHP

旧巷老猫 提交于 2019-11-30 02:14:41
问题 How can i do an array_merge on an associative array, like so: Array 1: $options = array ( "1567" => "test", "1853" => "test1", ); Array 2: $option = array ( "none" => "N/A" ); So i need to array_merge these two but when i do i get this (in debug): Array ( [none] => N/A [0] => test [1] => test1 ) 回答1: try using : $finalArray = $options + $option .see http://codepad.org/BJ0HVtac Just check the behaviour for duplicate keys, I did not test this. For unique keys, it works great. <?php $options =

PHP: array_merge() in alternate order (zip order)?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 17:24:00
Is there a native PHP function to zip merge two arrays? Look at the following example: $a = array("a","b","c"); $b = array("d","e","f"); $c = array("g","h","i"); var_dump(array_merge($a,$b,$c)); This produces: array(9) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" [5]=> string(1) "f" [6]=> string(1) "g" [7]=> string(1) "h" [8]=> string(1) "i" } However I want: array(9) { [0]=> string(1) "a" [1]=> string(1) "d" [2]=> string(1) "g" [3]=> string(1) "b" [4]=> string(1) "e" [5]=> string(1) "h" [6]=> string(1) "c" [7]=> string(1) "f" [8]=>

How to merge multidimensional arrays while preserving keys?

杀马特。学长 韩版系。学妹 提交于 2019-11-29 12:54:48
问题 Is there a way for these arrays $array1 = array( '21-24' => array( '1' => array("...") ) ); $array2 = array( '21-24' => array( '7' => array("..."), ) ); $array3 = array( '25 and over' => array( '1' => array("...") ) ); $array4 = array( '25 and over' => array( '7' => array("...") ) ); to be merged which result into the array below? array( '21-24' => array( '1' => array("..."), '7' => array("...") ), '25 and over' => array( '1' => array("..."), '7' => array("...") ) ); NOTE : I don't have

array_merge & array_unique

爱⌒轻易说出口 提交于 2019-11-29 10:14:01
Is there an array function in PHP that somehow does array_merge, comparing the values , ignoring the keys? I think that array_unique(array_merge($a, $b)) works, however I believe there must be a nicer way to do this. eg. $a = array(0 => 0, 1 => 1, 2 => 2); $b = array(0 => 2, 1 => 3, 2 => 4); resulting in: $ab = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4); Please note that I don't care about the keys in $ab , however it would be nice if they were ascending, starting at 0 to count($ab)-1 . The most elegant, simple, and efficient solution is the one mentioned in the original question... $ab =

I want to add sub arrays to one single array in php [duplicate]

谁说我不能喝 提交于 2019-11-29 03:45:06
问题 This question already has answers here : How to Flatten a Multidimensional Array? (28 answers) Closed 6 years ago . i have array like this......... Array ( [0] => Array ( [0] => rose [1] => monkey [2] => donkey ) [1] => Array ( [0] => daisy [1] => monkey [2] => donkey ) [2] => Array ( [0] => orchid [1] => monkey [2] => donkey ) ) and i want like this......... Array ( [0] => rose [1] => monkey [2] => donkey [3] => daisy [4] => monkey [5] => donkey [6] => orchid [7] => monkey [8] => donkey ) ..

php array_merge_recursive preserving numeric keys

99封情书 提交于 2019-11-29 02:00:22
问题 I would simply like to merge $a = array("59745506"=>array("up" => 0,)); $b = array("59745506"=>array("text" => "jfrj")); $c = array_merge_recursive_new($a, $b); result: Array ( [0] => Array ( [up] => 0 ) [1] => Array ( [text] => jfrj ) ) expected result: Array ( [59745506] => Array ( [up] => 0 [text] => jfrj ) ) the 2nd comment in http://www.php.net/manual/en/function.array-merge-recursive.php is working, is it the best solution for my case (where I need to merge arrays with multiple numeric

php array_merge associative arrays

☆樱花仙子☆ 提交于 2019-11-28 22:56:19
I'm trying to prepend an item to the beginning of an associative array. I figured the best way to do this is to use array_merge, but I'm having some odd consequences. I get the id and Name of products from a mysql database, and it gets returned as an associative array, like this (not the actual data coming back, but sample data for this question that represents what the data looks like approximately): $products = array (1 => 'Product 1', 42 => 'Product 42', 100 => 'Product 100'); this is getting sent to an html helper to create a dropdown that associates the key with the value, and the value

Merge values from different arrays to one with the same key

走远了吗. 提交于 2019-11-28 14:40:57
I have two arrays: Array ( [0] => 5 [1] => 4 ) Array ( [0] => BMW [1] => Ferrari ) And I would like to have that result. Merge the values with the same key Array ( [0] => Array ( [0] => 5 [1] => BMW ) [1] => Array ( [0] => 4 [1] => Ferrari ) ) How could I do that? Is there any native PHP function that does this? I tried array_merge_recursive and array_merge but did not get the expected result As to @splash58 comment: You can use array-map . Here an example: $array = [["5", "4"], ["BMW", "Ferrari"]]; $res = array_map(null, ...$array); Now res will contain: Array ( [0] => Array ( [0] => 5 [1] =>

PHP: array_merge() in alternate order (zip order)?

孤街浪徒 提交于 2019-11-28 12:29:56
问题 Is there a native PHP function to zip merge two arrays? Look at the following example: $a = array("a","b","c"); $b = array("d","e","f"); $c = array("g","h","i"); var_dump(array_merge($a,$b,$c)); This produces: array(9) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" [5]=> string(1) "f" [6]=> string(1) "g" [7]=> string(1) "h" [8]=> string(1) "i" } However I want: array(9) { [0]=> string(1) "a" [1]=> string(1) "d" [2]=> string(1) "g" [3]=>