resting Posted October 11, 2009 Share Posted October 11, 2009 Lets say I have this array array[0] => array(['prod_id']=1, ['image'] = 'a.jpg') array[1] => array(['prod_id']=1, ['image'] = 'b.jpg') array[2] => array(['prod_id']=2, ['image'] = 'c.jpg') array[3] => array(['prod_id']=2, ['image'] = 'd.jpg') array[4] => array(['prod_id']=3, ['image'] = 'e.jpg') array[5] => array(['prod_id']=3, ['image'] = 'f.jpg') how do i compare all of them and extract only the 1st occurrence of the ['image'] value where the ['prod_id'] are the same? Quote Link to comment https://forums.phpfreaks.com/topic/177271-solved-comparing-multi-arrays/ Share on other sites More sharing options...
Psycho Posted October 11, 2009 Share Posted October 11, 2009 function returnFirstImage($inputArray) { $outputArray = array(); foreach($inputArray as $record) { if(!isset($outputArray[$record['prod_id']]) { $outputArray[$record['prod_id']] = $record['image'] } } return $outputArray; } Quote Link to comment https://forums.phpfreaks.com/topic/177271-solved-comparing-multi-arrays/#findComment-934679 Share on other sites More sharing options...
resting Posted October 11, 2009 Author Share Posted October 11, 2009 function returnFirstImage($inputArray) { $outputArray = array(); foreach($inputArray as $record) { if(!isset($outputArray[$record['prod_id']]) { $outputArray[$record['prod_id']] = $record['image'] } } return $outputArray; } a bit of typo, but it works great. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/177271-solved-comparing-multi-arrays/#findComment-934696 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.