array-merge

Combine 2 arrays by matching the same foreign key

僤鯓⒐⒋嵵緔 提交于 2019-12-02 17:43:11
问题 I have 2 table, that is a question_table and answer_table the structure is like this : And I have a JSON array that I got from question_table and answer_table using php like this. // to get the question $pertanyaan = "select * from question_table”; $resultPertanyaan = mysqli_query($con, $pertanyaan); while($rowQuery= mysqli_fetch_array($resultPertanyaan)){ $array_question[]= array('id'=>$rowQuery['id’],’question’=>$rowQuery['question']); } and the result is like this array_question : [ { "id"

Convert 1D Array to 2D Array and Join PHP

孤者浪人 提交于 2019-12-02 10:26:05
I have this unidimensional array: $array1: Array ( [coupon_code] => GTY777R [coupon_description] => Credito $5 USD ) $array2: (2D Array) Array ( [0] => Array ( [coupon_code] => 0000000 [coupon_description] => Intenta de nuevo ) [1] => Array ( [coupon_code] => 0000000 [coupon_description] => Intenta de nuevo ) ) I need to check if $array1 is a Unidimentional Array and convert it before join, for example: if (is_1D($array1) = TRUE) { $array1 = convert_2D($array1); } $array3 = join_arrays($array1, $array2); Final Result $array1 converted to 2D and joined: $array3 Array ( [0] => Array ( [coupon

Array and Associative Array Merge

£可爱£侵袭症+ 提交于 2019-12-02 08:59:38
问题 How can I achieve the third array, merging Array1 and Array2? What's the best way to do that in PHP? Many thanks. Array2 has like index (key), the associative value of id in Array1. Array1 Array ( [0] => Array ( [id] => 56 [grade] => 6.7 ) [1] => Array ( [id] => 214 [grade] => 3.2 ) ) Array2 Array ( [56] => 2.4 [214] => 5.8 ) Result wanted Array ( [0] => Array ( [id] => 56 [grade] => 2.4 ) [1] => Array ( [id] => 214 [grade] => 5.8 ) ) 回答1: foreach($array1 as &$arrayItem) { $arrayItem['grade']

Array and Associative Array Merge

倖福魔咒の 提交于 2019-12-02 04:32:02
How can I achieve the third array, merging Array1 and Array2? What's the best way to do that in PHP? Many thanks. Array2 has like index (key), the associative value of id in Array1. Array1 Array ( [0] => Array ( [id] => 56 [grade] => 6.7 ) [1] => Array ( [id] => 214 [grade] => 3.2 ) ) Array2 Array ( [56] => 2.4 [214] => 5.8 ) Result wanted Array ( [0] => Array ( [id] => 56 [grade] => 2.4 ) [1] => Array ( [id] => 214 [grade] => 5.8 ) ) foreach($array1 as &$arrayItem) { $arrayItem['grade'] = $array2[$arrayItem['id']] } Here you will have array 1 merged as you wished 来源: https://stackoverflow.com

PHP array merge from unknown number of parameters

我们两清 提交于 2019-12-01 11:04:45
问题 I have an PHP array looking like this: $array['my_data']['value'] = 'some value'; $array['my_own_data']['value'] = 'another value'; $array['different_data']['value'] = 'another value'; I need to use array_merge in PHP or such. The problem is that the number of first level keys are unknown. It could be 150 items, I don't know. This would not work for me because of the unknown number of keys: array_merge($array['my_data'], $array['my_own_data'], $array['different_data']); Do I need a loop or

How to combine array of array values into one array?

牧云@^-^@ 提交于 2019-12-01 00:22:20
I have following array, Array ( [14289] => Array ( [0] => Ability||STROKE CLINIC,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child1||FC [1] => Ability||N/S++,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child2||SC [2] => Ability||B-,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child3||TC ) [14279] => Array ( [0] => Ability||STROKE CLINIC,Session||Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time||#1 only: 1pm,child1||FC [1] => Ability||N/S++,Session||Session #1: Tues May 31st -

How to combine array of array values into one array?

人盡茶涼 提交于 2019-11-30 19:30:01
问题 I have following array, Array ( [14289] => Array ( [0] => Ability||STROKE CLINIC,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child1||FC [1] => Ability||N/S++,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child2||SC [2] => Ability||B-,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child3||TC ) [14279] => Array ( [0] => Ability||STROKE CLINIC,Session||Session #1: Tues May 31st - Fri June

Array-Merge on an associative array in PHP

只愿长相守 提交于 2019-11-30 18:14:21
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 ) 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 = array ( "1567" => "test", "1853" => "test1", ); $option = array ( "none" => "N/A" ); $final = array_merge(

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

半城伤御伤魂 提交于 2019-11-30 05:24:59
This question already has an answer here: How to Flatten a Multidimensional Array? 27 answers 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 ) ....I used array merge but it's not working because my array generates dymaically and each time shows different arrays. problem

Merging two arrays by index

落爺英雄遲暮 提交于 2019-11-30 04:47:00
问题 Okay, if feel like this should be really simple and accomplished by a function like array_merge() or array_merge_recursive , but I can't quite figure it out. I have two simple arrays structured like the (simplified) example below. I simply want to merge them into one array based on their index. $array 1: Array ( [0] => Array ( [ID] => 201 [latLng] => 45.5234515, -122.6762071 ) [1] => Array ( [ID] => 199 [latLng] => 37.7931446, -122.39466520000002 ) ) et cetera… $array2 : Array ( [0] => Array