SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question removing duplicated key in multidimensional array...

    Dear all,

    Do you have any idea to remove duplicated key by comparing the value of listing_id in the multidimensional array?

    array(4) {
    [0]=>
    array(10) {
    ["id"]=>
    string(1) "1"

    ["listing_id"]=>
    string(4) "4129"

    }
    [1]=>
    array(10) {
    ["id"]=>
    string(1) "2"

    ["listing_id"]=>
    string(4) "4129"

    }
    [2]=>
    array(10) {
    ["id"]=>
    string(1) "4"
    ["listing_id"]=>
    string(2) "26"
    }
    [3]=>
    array(10) {
    ["id"]=>
    string(1) "5"
    ["listing_id"]=>
    string(4) "5329"
    }
    }
    I tested using the code below but is not working as my array has unique id...

    function multi_unique($array) {
    foreach ($array as $k=>$na)
    $new[$k] = serialize($na);
    $uniq = array_unique($new);
    foreach($uniq as $k=>$ser)
    $new1[$k] = unserialize($ser);
    return ($new1);
    }
    newbie...

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What da...?
    Why serialize?

    The best solution whould be to create a new array and use listing_id as array key. This way it will be guaranteed to be unique since array keys must be unique.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by lampcms.com View Post
    What da...?
    Why serialize?

    The best solution whould be to create a new array and use listing_id as array key. This way it will be guaranteed to be unique since array keys must be unique.

    Hi lampcms.com,

    I serialize it because array_unique() is not working for multidimensional array. However, this method will only work if the id & listing_id having the same value after serialization.

    I wish to remove the array that having same listing_id from the list.

    Is there any other way to do this besides creating a new array?

    Thank you!
    newbie...

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You just need to keep track of listing_id while looping over array, then skip elements, anyway, like this.
    $aTemp = $aNew = array();
    foreach($a as $key=>$aVal){
    if(array_key_exists($aVal['listing_id'], $atemp){
    continue;
    }
    $aTemp[$aVal['listing_id'] = 1;
    $aNew[] = $aVal;
    }

    The $aNew is the new array with duplicates removed
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by lampcms.com View Post
    You just need to keep track of listing_id while looping over array, then skip elements, anyway, like this.
    $aTemp = $aNew = array();
    foreach($a as $key=>$aVal){
    if(array_key_exists($aVal['listing_id'], $atemp){
    continue;
    }
    $aTemp[$aVal['listing_id'] = 1;
    $aNew[] = $aVal;
    }

    The $aNew is the new array with duplicates removed
    Thanks lampcms.com...

    Actually my situation is a bit complicated...

    The array I shown above is actually pull from database:

    $results = $db->GetAll("SELECT o.id, o.listing_id, o.title, o.description
    FROM ".T_OFFERS." o INNER JOIN ".T_LISTINGS." l ON o.listing_id=l.id
    WHERE l.status='active'");
    I trying to use Distinct to solve the problem but not working yet. So I wonder whether is there any solution in php...
    newbie...

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use PDO, it has a special little-known fetch mode PDO::FETCH_GROUP
    The results will be then grouped by the first column, in your case by listing_id

    So if there are multiple rows with the same listing_id they will all be elements of the array and you can then just use the first element (with key 0)
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  7. #7
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lampcms.com View Post
    If you use PDO, it has a special little-known fetch mode PDO::FETCH_GROUP
    The results will be then grouped by the first column, in your case by listing_id

    So if there are multiple rows with the same listing_id they will all be elements of the array and you can then just use the first element (with key 0)

    Oh, PDO is very new to me...

    Anyway, really thank you for your suggestions!

    Appreciate for your kind sharing!
    newbie...

  8. #8
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what's is this GetAll()?
    Looks like you are using some sort of database library. Which one is it?
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  9. #9
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I'm playing with the phpmydirectory system...
    Some of the scripts are encrypted.

    After GetAll(); we use foreach loop to get the result printed...

    (Did I answer your question? )
    newbie...

  10. #10
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use SELECT DISTINCT in your SQL to grab only unique results with your SQL in the first place.

  11. #11
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by baseballdude_ View Post
    You could use SELECT DISTINCT in your SQL to grab only unique results with your SQL in the first place.
    Thanks baseballdude,

    I solved it by adding a "group by" in my sql query
    newbie...

  12. #12
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lampcms.com View Post
    You just need to keep track of listing_id while looping over array, then skip elements, anyway, like this.
    $aTemp = $aNew = array();
    foreach($a as $key=>$aVal){
    if(array_key_exists($aVal['listing_id'], $atemp){
    continue;
    }
    $aTemp[$aVal['listing_id'] = 1;
    $aNew[] = $aVal;
    }

    The $aNew is the new array with duplicates removed



    Hi lampcms.com,

    The Second Part of My Question:

    Although I have solved the previous problem using "group by" in the sql query, but I encounter another problem after using array_merge.

    Thus, I tried to applied your suggestion:

    Code:
    $aTemp = $aNew = array();
    		foreach($results as $key=>$aVal){
    		if(array_key_exists($aVal['listing_id'], $atemp)){
    		continue;
    		}else{
    		$aTemp[$aVal['listing_id']] = 1;
    		$aNew[] = $aVal;
    		}
    		}
    Below is the out put of var_dump($aNew);


    Code:
    array(3) {
    	[0]=>
    	array(10) {
    		["id"]=>
    		string(1) "1"
    		["listing_id"]=>
    		string(4) "4129"
    		["date"]=>
    		string(4) "2009-11-30"
    	}
    	[1]=>
    		array(10) {
    		["id"]=>
    		string(1) "2"
    		["listing_id"]=>
    		string(4) "4129"
    		["date"]=>
    		string(4) "2009-11-28"
    	}
    	[2]=>
    		array(10) {
    		["id"]=>
    		string(1) "4"
    		["listing_id"]=>
    		string(2) "26"
    		["date"]=>
    		string(4) "2009-12-02"
    	}
    }

    Do I make any mistake in the code?
    newbie...

  13. #13
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Code:
    array(3) {
    	[0]=>
    	array(10) {
    		["id"]=>
    		string(1) "1"
    		["listing_id"]=>
    		string(4) "4129"
    		["date"]=>
    		string(4) "2009-11-30"
    	}
    	[1]=>
    		array(10) {
    		["id"]=>
    		string(1) "2"
    		["listing_id"]=>
    		string(4) "4129"
    		["date"]=>
    		string(4) "2009-11-28"
    	}
    	[2]=>
    		array(10) {
    		["id"]=>
    		string(1) "4"
    		["listing_id"]=>
    		string(2) "26"
    		["date"]=>
    		string(4) "2009-12-02"
    	}
    }
    Actually is it possible to remove the duplicated array like this?
    newbie...

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You should remove the duplicates as a part of the SQL query to the database.

    If you can provide some sample database data, we can advise on how to most effectively retrieve what you require.

    Failing that, you can use an array when using the data. Set a value in the array using the listingid number as the key, so that if that key number already exists you know you can safely ignore that one.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by pmw57 View Post
    You should remove the duplicates as a part of the SQL query to the database.

    If you can provide some sample database data, we can advise on how to most effectively retrieve what you require.

    Failing that, you can use an array when using the data. Set a value in the array using the listingid number as the key, so that if that key number already exists you know you can safely ignore that one.
    The condition is like this:

    I got the arrays from 2 search:

    $result1=mysql_query("select * from listing where title='%".$keyword."%'");

    $result1 will generate data such as:

    Code:
    array(2) {
      [0]=>
      array(93) {
        ["id"]=>
        string(2) "26"
        ["listing_id"]=>
        string(2) "26"
        ["last_modify"]=>
        string(2) "2009-09-20"
      }
      [1]=>
      array(93) {
        ["id"]=>
        string(4) "4129"
        ["listing_id"]=>
        string(4) "4129"
        ["last_modify"]=>
        string(2) "2009-11-28"
      }
    }
    $result2=mysql_query("select * from product where product_name='%".$keyword."%'");

    $result2 will generate data such as:

    Code:
    array(2) {
      [0]=>
      array(93) {
        ["id"]=>
        string(2) "288"
        ["listing_id"]=>
        string(2) "288"
        ["last_modify"]=>
        string(2) "2009-10-20"
      }
      [1]=>
      array(93) {
        ["id"]=>
        string(4) "4129"
        ["listing_id"]=>
        string(4) "4129"
        ["last_modify"]=>
        string(2) "2009-09-30"
      }
    }
    So the search result will be:

    $result=$result1.$result2;

    However, I don't want to echo the same data twice as you see the both results contains listing_id = 4129

    So i thinking is there any way I can remove the duplicated data from the merged array $result
    newbie...

  16. #16
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    PHP Code:
    mysql_query(sprintf(
           
    "SELECT 
                 id
                 ,listing_id
                 ,last_modify
              FROM
                 listing
             WHERE
                 title LIKE '%s'
             UNION
            SELECT
                 id
                 ,listing_id
                 ,last_modify
              FROM
                 product
             WHERE
                 product_name LIKE '%s'"
        
    ,"%$keyword%"
        
    ,"%$keyword%"
    )); 

  17. #17
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The following is written from the top of my head, until I can get to a php interpreter later on tonight.

    The general idea is to check each $result2 item against each of the $result1 items. If the listing_id is the same then that duplicate will not be pushed on to the $result1 array.

    Code php:
    for ($i = 0; $i < count($result2); $i++) {
        $duplicateFound = FALSE;
        for ($j = 0; $j < count($result1); $j++) {
            if ($result1[j]['listing_id'] === $result2[i]['listing_id']) {
                $duplicateFound = TRUE;
            }
        }
        if (!$duplicateFound) {
            array_push($result1, $result2[j]);
        }
    }
    // dump $result 2, keep $result 1.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  18. #18
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oddz & pmw57,

    Thanks a lot for the suggestion....

    I'll try it out soon and hope to update you guys which is working for me...

    Really thanks a lot! Apreciate it...
    newbie...


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
  •