SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comparing multi arrays

    Lets say I have this array

    PHP Code:
    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?

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    This would be done in a while loop. However, I might just be tired but Im not understanding when you want the loop to end.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Location
    Brisbane, Australia
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One-liner;
    PHP Code:
    array_reverse(call_user_func_array('array_combine'call_user_func_array('array_merge_recursive'array_reverse($array)))); 

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll guess this data comes from a database, so you should consider issuing the proper query.

  5. #5
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for($i count($arr) - 1$i$i--)
    {
       
    $arr["_{$arr[$i]['prod_id']}"] = $arr[$i]['image'];
       unset(
    $arr[$i]);



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •