smith.james0 Posted August 10, 2018 Share Posted August 10, 2018 Hi, It's been a few years since i've done any php coding and it seams like I have forgotten how to combine 2 arrays! I am trying to combine This .... Array ( [0] => Array ( [0] => 6 [1] => 12.25000000000000 [2] => 14.50000000000000 ) [1] => Array ( [0] => 7 [1] => 12.43700000000000 [2] => 13.12500000000000 ) } etc, etc and this .... Array ( [6] => 11.00000000000000 [7] => 11.31200000000000 [17] => 14.18700000000000 } etc etc to make ... Array ( [0] => Array ( [0] => 6 [1] => 12.25000000000000 [2] => 14.50000000000000 [3] => 11.00000000000000 ) [1] => Array ( [0] => 7 [1] => 12.43700000000000 [2] => 13.12500000000000 [3] => 11.31200000000000 ) [2] => Array ( [0] => 17 [1] => 16.12500000000000 [2] => 15.75000000000000 [3] => 14.18700000000000 ) } I've tried $out2 = array(); foreach ($out as $value){ $out2[] = array_merge($value, array($water[$key])); } but this seams to alter the value of the combined array value in the 3rd key. Can anyone point to what I have done wrong? Regards Quote Link to comment https://forums.phpfreaks.com/topic/307601-array-help/ Share on other sites More sharing options...
NotionCommotion Posted August 10, 2018 Share Posted August 10, 2018 There are many ways, but the following should work. $arr2=array_values($arr2); foreach($arr2 as $key=>$value){ $arr1[$key][]=$value; } While array_merge() seems like what you want, if you don't want to reindex the keys, use $arr1+$arr2. Quote Link to comment https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560310 Share on other sites More sharing options...
ginerjm Posted August 10, 2018 Share Posted August 10, 2018 (edited) Not sure I agree with Notion's post. First - line 1 seems irrelevant. And from my look at your post OP, you have an array of individual arrays followed by an array of individual elements with numeric keys. When you add the second array to the first array - how do you want to "fit" them in? I think you will quickly run into duplicate keys and start losing matched data as I see it. Do you want each element of array #2 to be a single entry in the first array? If so how do you want to handle the key of this new element? Or do you want to add each element of array 2 as a one-element array in array 1 with a new sequentially assigned index for that new array item in array1? Edited August 10, 2018 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560311 Share on other sites More sharing options...
benanamen Posted August 10, 2018 Share Posted August 10, 2018 I smell an XY Problem. OP, what is the REAL problem you are trying to solve, not your attempt at solving it. Where are these arrays coming from in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560312 Share on other sites More sharing options...
smith.james0 Posted August 10, 2018 Author Share Posted August 10, 2018 Hi, The arrays come from three sql queries. I have already combined two which output to a graph to show temperature over a 24 hour period, now I have added another temperature probe and I want to add the third array to the existing one. I don't know a way of querying the db to produce a array like this Array ( [0] => Array ( [0] => 6 (Hour from db) [1] => 12.25000000000000 (Temp probe 1) [2] => 14.50000000000000 (Temp probe 2) [3] => 11.00000000000000 (Temp probe 3) ) etc etc.. Regards Quote Link to comment https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560313 Share on other sites More sharing options...
benanamen Posted August 10, 2018 Share Posted August 10, 2018 Just as I suspected. Post an SQL dump of your DB with a few sample records. Forgot about technical jargon. Just describe in detail what you want to know/show about the data. Quote Link to comment https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560314 Share on other sites More sharing options...
alpine Posted August 11, 2018 Share Posted August 11, 2018 (edited) Assuming the common identifier in the first array always has the first position: <?php foreach( $arr_1 as $k => $data ) { if ( isset( $arr_2[$data[0]] ) ) { $arr_1[$k][] = $arr_2[$data[0]]; } } ?> This will add data from the second array to the first array Edited August 11, 2018 by alpine Description added Quote Link to comment https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560315 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.