array-merge

Merging arrays based on a value of the key

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-20 06:02:47
问题 I have two arrays of arrays that have an id key, and I'd like to merge the data together based on that array's key and key value. The data would look something like: $color = [ ['id' => 1, 'color' => 'red'], ['id' => 2, 'color' => 'green'], ['id' => 3, 'color' => 'blue'], ]; $size = [ ['id' => 1, 'size' => 'SM'], ['id' => 2, 'size' => 'XL'], ['id' => 3, 'size' => 'MD'], ['id' => 4, 'size' => 'LG'], ]; $combined = [ ['id' => 1, 'color' => 'red', 'size' => 'SM'], ['id' => 2, 'color' => 'green',

Merging arrays based on a value of the key

别来无恙 提交于 2020-01-20 06:01:11
问题 I have two arrays of arrays that have an id key, and I'd like to merge the data together based on that array's key and key value. The data would look something like: $color = [ ['id' => 1, 'color' => 'red'], ['id' => 2, 'color' => 'green'], ['id' => 3, 'color' => 'blue'], ]; $size = [ ['id' => 1, 'size' => 'SM'], ['id' => 2, 'size' => 'XL'], ['id' => 3, 'size' => 'MD'], ['id' => 4, 'size' => 'LG'], ]; $combined = [ ['id' => 1, 'color' => 'red', 'size' => 'SM'], ['id' => 2, 'color' => 'green',

Flatten Array: Keep index, value equal to position in array

懵懂的女人 提交于 2020-01-14 19:00:31
问题 I've been having a little trouble trying to flatten arrays in a specific way. Here is a print_r view of the array I want to flatten: Array ( [1] => Array ( [8] => 1 [9] => 2 [10] => Array ( [15] => Array ( [22] => 1 ) [21] => 2 ) [11] => Array ( [16] => Array ( [23] => 1 ) ) ) [2] => Array ( [12] => 1 ) [3] => Array ( [13] => 1 ) [4] => Array ( [14] => 1 ) [5] => 5 [6] => 6 [7] => 7 ) What I'm attempting to create is an array which keeps the above indexes, but the value is equal to it's

Merge multi-dimensional arrays and sum column values which share a common value in another column

天大地大妈咪最大 提交于 2020-01-14 10:23:28
问题 I have 3 arrays for storing posts,comments, and likes. These are the JSON strings: //comments JSON (stores user and comment points) $comments='[ { "user": "5", "points": "12" }, { "user": "2", "points": "1" }, { "user": "3", "points": "1" } ]'; //likes(stores user and likes point) $likes='[ { "user": "1", "points": 7 }, { "user": "4", "points": 4 }, { "user": "3", "points": 1 } ]'; //posts (stores user and post points) $posts='[ { "user": "1", "points": "6" }, { "user": "3", "points": "2" },

Merge multi-dimensional arrays and sum column values which share a common value in another column

孤人 提交于 2020-01-14 10:22:52
问题 I have 3 arrays for storing posts,comments, and likes. These are the JSON strings: //comments JSON (stores user and comment points) $comments='[ { "user": "5", "points": "12" }, { "user": "2", "points": "1" }, { "user": "3", "points": "1" } ]'; //likes(stores user and likes point) $likes='[ { "user": "1", "points": 7 }, { "user": "4", "points": 4 }, { "user": "3", "points": 1 } ]'; //posts (stores user and post points) $posts='[ { "user": "1", "points": "6" }, { "user": "3", "points": "2" },

how to merge two nested dictionaries under a same dictionary

陌路散爱 提交于 2020-01-13 14:56:25
问题 for example I have a dictionary: dictA={"nest1":{"01feb":[1,2,3,4,5],"02feb":[1,7,8,9,10]}, "nest2":{"01feb":[1,2,3,4,5],"02feb":[6,4,8,10,10]}} the list inside has the same length. I need to merge nest1 and nest2 as one dictionary, and the result should be like this: dictA={"nest":{"01feb":[2,4,6,8,10],"02feb":[7,11,16,19,20]}} 回答1: Plese find the below code for your query. dictA={"nest1":{"01feb":[1,2,3,4,5],"02feb":[1,7,8,9,10]}, "nest2":{"01feb":[1,2,3,4,5],"02feb":[6,4,8,10,10]}} result

Join two associative arrays with PHP

纵饮孤独 提交于 2020-01-06 09:55:00
问题 I'm trying to merge data from a Wordpress feed with Google Analytics API data to create a table of Wordpress post information including each post's page views. I think I can match on the post url to accomplish this but I am not sure how to do this. I've spent a bit of time to get the data from the two sources to look like this: print_r($analytics); returns: Array ( [0] => Array ( [url] => blog-post-url-121 [pageViews] => 34326 ) [1] => Array ( [url] => blog-post-url-245 [pageViews] => 14642 )

PHP merge values of numeric arrays in corresponding keys

孤者浪人 提交于 2020-01-04 09:05:11
问题 So consider an array with my 3 favorite fruits: $array1 = array("Apple", "Banana","Raspberry") I want to merge it with their own beautiful and natural color $array2 = array("Green ", "Yellow ","Red ") So that the results would look like ([0] => Green Apple [1] => Yellow Banane [2] => Red Raspberry) I need something to be scalable (2 to 6 keys, always the same between arrays) What I tried and results array_combine($array2,$array1) Result: Array ( [Green ] => Apple [Yellow ] => Banana [Red ] =>

array_merge changes the keys

雨燕双飞 提交于 2020-01-03 09:10:10
问题 I got the following array: $arr = array(6 => 'Somedata', 7 => 'Somedata1', 8 => 'Somedata2'); The problem is that, when I use array_merge( (array) "Select the data", $arr); , it does change the array keys into: Array ( [0] => Not specified [1] => Somedata [2] => Somedata1 [3] => Somedata2 ) Is that possible to skip the array_merge key preversion so the output would look like this? Array ( [0] => Not specified [6] => Somedata [7] => Somedata1 [8] => Somedata2 ) 回答1: Use the + operator to

How to merge two arrays by taking over only values from the second array that has the same keys as the first one?

狂风中的少年 提交于 2019-12-29 07:35:12
问题 I'd like to merge two arrays with each other: $filtered = array(1 => 'a', 3 => 'c'); $changed = array(2 => 'b*', 3 => 'c*'); Whereas the merge should include all elements of $filtered and all those elements of $changed that have a corresponding key in $filtered : $merged = array(1 => 'a', 3 => 'c*'); array_merge($filtered, $changed) would add the additional keys of $changed into $filtered as well. So it does not really fit. I know that I can use $keys = array_intersect_key($filtered, $changed