dsbpac Posted June 14, 2016 Share Posted June 14, 2016 I have two arrays that are listed like this. Array1 ( [0] => Array ( [32347] => 8 [22188] => 3 [37493] => 4 [37201] => 7 ) ) Array2 ( [0] => Array ( [0] => Array ( [0] => 32347 [1] => 28470 [2] => 35319 [3] => 37493 ) [1] => Array ( [0] => 5 [1] => 2 [2] => [3] => 4 ) [2] => Array ( [0] => 484 [1] => 383 [2] => 315 [3] => 320 ) ) ) I want to combine both arrays if value is the same. Example [32347] => 8 => 5 => 484 Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/301342-combine-two-arrays/ Share on other sites More sharing options...
ginerjm Posted June 14, 2016 Share Posted June 14, 2016 (edited) Have you looked at all of the array-related functions in the php manual? An excellent resource - you should bookmark it. Of course you need to look at the structure of your arrays closer. One is a 2-deep array and the other is 3-deep. What exactly do you want to merge and what kind of an array do you want to end up. Plus - you don't have like values between the arrays. One has rather unique values as the keys and the other has them as values. Edited June 14, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/301342-combine-two-arrays/#findComment-1533648 Share on other sites More sharing options...
dsbpac Posted June 14, 2016 Author Share Posted June 14, 2016 I actually did look in there. I already took several arrays and combined them into two seperate arrays. I'm pretty sure I need to do a nested foreach loop in order to do this. I just don't understand how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/301342-combine-two-arrays/#findComment-1533649 Share on other sites More sharing options...
maxxd Posted June 14, 2016 Share Posted June 14, 2016 Theoretically, yes, you can combine arrays in nested foreach() loops, but I'm not sure I understand what your end goal is, which makes it difficult to advise how one would possibly go about doing that. What's the correlation between the array values? Right now it seems utterly random. Quote Link to comment https://forums.phpfreaks.com/topic/301342-combine-two-arrays/#findComment-1533650 Share on other sites More sharing options...
ginerjm Posted June 14, 2016 Share Posted June 14, 2016 (edited) Yes! One seems to have real "value" oriented data as the values while the other seems to use those "values" as keys. Pluse your one array has arrays an extra layer deeper and neither of your two arrays identify the different child arrays separate from the others. Why such a blind structure? And if this is the entire set of data, you could have manually created the desired result by now. Edited June 14, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/301342-combine-two-arrays/#findComment-1533652 Share on other sites More sharing options...
dsbpac Posted June 14, 2016 Author Share Posted June 14, 2016 I changed array1 to be more structured now. [0] => Array1 ( [0] => 32347 [1] => 22188 [2] => 37493 [3] => 37056) [1] => Array ( [0] => 8 [1] => 3 [2] => 4 [3] => 7) Quote Link to comment https://forums.phpfreaks.com/topic/301342-combine-two-arrays/#findComment-1533653 Share on other sites More sharing options...
ginerjm Posted June 14, 2016 Share Posted June 14, 2016 (edited) Array1 as you call it is simply an element of a bigger array. What is the correlation between the named 'array1' element and the un-named element that is the sibling to it? I don't see it. You do realize that you have a multiple level array with elements ( the [0] and the [1] ) that have no identifiers to help any program to recognize what it is dealing with. As for merging the latest post with the second(?) array you have in your original post, how is the script suppose to connect those completely separate arrays? From your original post again - you have array1 that has one element. Period. Then you have array2 that has one element. In each of them you have arrays as the values of those single elements, except array1 has only one array under the child, while array2 has 3 arrays under its child. What is the purpose behind this? They don't have names so why are they are not simply all elements of the topmost array? Edited June 14, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/301342-combine-two-arrays/#findComment-1533658 Share on other sites More sharing options...
Psycho Posted June 15, 2016 Share Posted June 15, 2016 (edited) What is the source of these arrays? Are you retrieving data from a database? If so, there will definitely be a better way to combine the data than combining arrays. Also, as others have said, the data has no context. In your example of: [32347] => 8 => 5 => 484 What does the 8, 5 and 484 represent? Besides, your example seems to indicate that you want each value to be a sub array of the preceding value. Perhaps it should look more like this [32347] => (8, 5, 484) Or this: [32347] => ( 'time' =>8, 'amount' =>5, 'foo' => 484 ) With your original data, here is a possible solution <?php $array1 = array(32347 => 8, 22188 => 3, 37493 => 4, 37201 => 7); $array2 = array( array( array(32347, 28470, 35319, 37493), array(5, 2, '', 4), array(484, 383, 315, 320) ) ); $newArray = array(); foreach($array1 as $key => $value) { $newArray[$key][] = $value; foreach($array2 as $subArray) { //extract the first array index (with the keys) $keyArray = array_shift($subArray); //Get the index for the key - if exists $keyIndex = array_search($key, $keyArray); //If the key exists, get the other values in the sub array if($keyIndex !== false) { foreach($subArray as $valuesArray) { if(isset($valuesArray[$keyIndex])) { $newArray[$key][] = $valuesArray[$keyIndex]; } } } } } echo "<pre>" . print_r($newArray, 1) . "</pre>"; ?> Output Array ( [32347] => Array ( [0] => 8 [1] => 5 [2] => 484 ) [22188] => Array ( [0] => 3 ) [37493] => Array ( [0] => 4 [1] => 4 [2] => 320 ) [37201] => Array ( [0] => 7 ) ) Edited June 15, 2016 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/301342-combine-two-arrays/#findComment-1533700 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.