Scooby08 Posted January 29, 2010 Share Posted January 29, 2010 Hello.. I'm trying to figure out a way to combine these first two arrays to get the last array.. 1st array: Array ( [0] => zero [1] => one [2] => two ) 2nd array: Array ( [0] => Array ( [0] => test0 ) [1] => Array ( [0] => test1 ) [2] => Array ( [0] => test2 ) ) The array I'm trying to get out of both: Array ( [zero] => Array ( [0] => test0 ) [one] => Array ( [0] => test1 ) [two] => Array ( [0] => test2 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/190205-how-could-i-combine-these-arrays/ Share on other sites More sharing options...
trq Posted January 29, 2010 Share Posted January 29, 2010 Assuming the first is $a and the second $b. $new = array(); foreach ($a as $k=> $v) { $new[$v] = $b[$k]; } $new will be the new array. Quote Link to comment https://forums.phpfreaks.com/topic/190205-how-could-i-combine-these-arrays/#findComment-1003491 Share on other sites More sharing options...
Alex Posted January 29, 2010 Share Posted January 29, 2010 Or.. $newarr = array_combine(array_values($arr1), array_values($arr2)); Quote Link to comment https://forums.phpfreaks.com/topic/190205-how-could-i-combine-these-arrays/#findComment-1003495 Share on other sites More sharing options...
Scooby08 Posted January 29, 2010 Author Share Posted January 29, 2010 Thanks for the quick responses!! That last one gave me this error: Warning: array_combine() [function.array-combine]: Both parameters should have an equal number of elements Quote Link to comment https://forums.phpfreaks.com/topic/190205-how-could-i-combine-these-arrays/#findComment-1003504 Share on other sites More sharing options...
Alex Posted January 29, 2010 Share Posted January 29, 2010 The error is pretty self-explanatory, the arrays aren't the same lengths like your gave in your example. So clearly it won't work, you need to have the same amount of keys as elements, right? Quote Link to comment https://forums.phpfreaks.com/topic/190205-how-could-i-combine-these-arrays/#findComment-1003531 Share on other sites More sharing options...
Scooby08 Posted January 29, 2010 Author Share Posted January 29, 2010 Indeed you are correct.. I had thought they were the same, but they aren't.. Everything I try runs into another dead end.. Thanks again guys! Quote Link to comment https://forums.phpfreaks.com/topic/190205-how-could-i-combine-these-arrays/#findComment-1003540 Share on other sites More sharing options...
satya61229 Posted January 29, 2010 Share Posted January 29, 2010 but thorpe code is working for you! I have also tested it! <?php $a = array('zero','one','two'); $b = array(array('test0'),array('test1'), array('test2')); echo '<pre>'; $a_val = array_values($a); $b_val = array_values($b); foreach ($a_val as $k=>$v) { $new[$v] = $b_val[$k]; } print_r($new); Quote Link to comment https://forums.phpfreaks.com/topic/190205-how-could-i-combine-these-arrays/#findComment-1003585 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.