SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast stationmaster's Avatar
    Join Date
    May 2003
    Location
    barcelona
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Removing duplicates from a Multidimensional array

    Hello Sitepoint,

    I am trying to ensure that each item of an array is unique.
    I have tried to use array_unique, but it doesn't work with multidimensionals,
    I have been looking for a solution, any pointers would be greatly appreciated.
    Thanx,

    example array start:
    Code PHP:
    array(20) { 
    [0]=>  object(stdClass)#207 (35) { ["ID"]=>  string(1) "7" ....  } 
    [1]=>  object(stdClass)#206 (35) { ["ID"]=>  string(1) "8" .... } 
    [2]=>  object(stdClass)#206 (35) {  ["ID"]=>  string(1) "7" .... } 
    [3]=>  object(stdClass)#206 (35) { ["ID"]=>  string(1) "9" ....  } 
    [4]=>  object(stdClass)#206 (35) { ["ID"]=>  string(1) "8" .... }
    etc. }


    example array finished:
    Code PHP:
    array(20) { 
    [0]=>  object(stdClass)#207 (35) { ["ID"]=>  string(1) "7" ....  } 
    [1]=>  object(stdClass)#206 (35) { ["ID"]=>  string(1) "8" .... } 
    [2]=>  object(stdClass)#206 (35) {  ["ID"]=>  string(1) "9" .... } 
    etc. }
    Daiv Mowbray twincascos Barcelona
    wordpress pluginswp-superslider.com
    portfolio portfolio.daivmowbray.com
    personal blogwww.daivmowbray.com

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you really have a multidimensional array? You posted an array of objects.

  3. #3
    SitePoint Enthusiast stationmaster's Avatar
    Join Date
    May 2003
    Location
    barcelona
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, crmalibu
    looks like my bad,
    should edit to say :
    array of objects.
    I thought it was a multidimensional
    Daiv Mowbray twincascos Barcelona
    wordpress pluginswp-superslider.com
    portfolio portfolio.daivmowbray.com
    personal blogwww.daivmowbray.com

  4. #4
    SitePoint Enthusiast stationmaster's Avatar
    Join Date
    May 2003
    Location
    barcelona
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, so my array of objects should be able to handle array_unique.
    how ever, when I use the array_unique,
    Code PHP:
    $myarray = array_unique($myarray);

    My error log spits this out:

    PHP Catchable fatal error: Object of class stdClass could not be converted to string

    So I guess I need to check for duplicates in $myarray -> OBJECT -> ID
    or something like that.
    Daiv Mowbray twincascos Barcelona
    wordpress pluginswp-superslider.com
    portfolio portfolio.daivmowbray.com
    personal blogwww.daivmowbray.com

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the SORT_REGULAR flag.


    But...I'm going to make a guess that this data comes from a database. If so, you should probably let the database worry about this uniqueness.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Maybe I'm missing something obvious, but you could just filter the array yourself:

    PHP Code:
    $unique = array();
    foreach (
    $myarray as $object) {
        if (isset(
    $unique[$object->ID])) {
            continue;
        }
        
    $unique[$object->ID] = $object;

    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Enthusiast stationmaster's Avatar
    Join Date
    May 2003
    Location
    barcelona
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, crmalibu,
    this is an array of OBJECTS pulled from a wordpress database.

    And thanx Salathe your suggestion does work as prescribed.


    The following is the query to the WP databse,
    I am adding an option to an open source plugin: superslider-show.
    the option is to be able to add "category:5" where the category number is what I want to know, then pull the ID of all posts in that category, which I then pull all image attachments from each post.

    If I can improve the query to reduce php code, great, as I'm a novice.

    PHP Code:
                $catid explode('category:'$id); 

                
    $catid $catid[1];
                
            global 
    $wpdb;          
                
    $all_attachments = array();
                
    $querystr "
       SELECT * FROM 
    $wpdb->posts
                LEFT JOIN 
    $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
                LEFT JOIN 
    $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
                LEFT JOIN 
    $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                WHERE 
    $wpdb->term_taxonomy.term_id IN (".$catid.")
                AND 
    $wpdb->term_taxonomy.taxonomy = 'category'
                AND 
    $wpdb->posts.post_type = 'post' 
                AND 
    $wpdb->posts.post_status = 'publish'
                ORDER BY 
    $wpdb->posts.post_date DESC
                 "
    ;
                 
    $categoryposts $wpdb->get_results($querystrOBJECT);//ARRAY_A  ARRAY_N OBJECT 
    Daiv Mowbray twincascos Barcelona
    wordpress pluginswp-superslider.com
    portfolio portfolio.daivmowbray.com
    personal blogwww.daivmowbray.com


Tags for this Thread

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
  •