Jump to content

[SOLVED] Comparing multi arrays


resting

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/177271-solved-comparing-multi-arrays/
Share on other sites

function returnFirstImage($inputArray)
{
    $outputArray = array();

    foreach($inputArray as $record)
    {
        if(!isset($outputArray[$record['prod_id']])
        {
            $outputArray[$record['prod_id']] = $record['image']
        }
    }

    return $outputArray;
}

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.