1

print_r($jobtypes) give me an array. Array contains duplicate elements. I want to remove the duplicates. Here is an array

                Array
(
    [0] => stdClass Object
        (
            [term_id] => 40
            [name] => Babysitting
            [slug] => babysitting
            [term_group] => 0
            [term_taxonomy_id] => 40
            [taxonomy] => job_listing_type
            [description] => 
            [parent] => 0
            [count] => 3
        )

    [1] => stdClass Object
        (
            [term_id] => 43
            [name] => Lawn Mowing
            [slug] => lawn-mowing
            [term_group] => 0
            [term_taxonomy_id] => 43
            [taxonomy] => job_listing_type
            [description] => 
            [parent] => 0
            [count] => 3
        )

    [2] => stdClass Object
        (
            [term_id] => 39
            [name] => Leaf Raking
            [slug] => leaf-raking
            [term_group] => 0
            [term_taxonomy_id] => 39
            [taxonomy] => job_listing_type
            [description] => 
            [parent] => 0
            [count] => 2
        )

    [3] => stdClass Object
        (
            [term_id] => 41
            [name] => Pet Sitting
            [slug] => pet-sitting
            [term_group] => 0
            [term_taxonomy_id] => 41
            [taxonomy] => job_listing_type
            [description] => 
            [parent] => 0
            [count] => 3
        )

    [4] => stdClass Object
        (
            [term_id] => 42
            [name] => Plant Watering
            [slug] => plant-watering
            [term_group] => 0
            [term_taxonomy_id] => 42
            [taxonomy] => job_listing_type
            [description] => 
            [parent] => 0
            [count] => 2
        )

    [5] => stdClass Object
        (
            [term_id] => 44
            [name] => Snow Shoveling
            [slug] => snow-shoveling
            [term_group] => 0
            [term_taxonomy_id] => 44
            [taxonomy] => job_listing_type
            [description] => 
            [parent] => 0
            [count] => 5
        )

    [6] => stdClass Object
        (
            [term_id] => 40
            [name] => Babysitting
            [slug] => babysitting
            [term_group] => 0
            [term_taxonomy_id] => 40
            [taxonomy] => job_listing_type
            [description] => 
            [parent] => 0
            [count] => 3
            [filter] => raw
        )

)

How to remove the duplicates. I this case

[name] => Babysitting
                [slug] => babysitting
                [term_group] => 0
                [term_taxonomy_id] => 40
                [taxonomy] => job_listing_type
                [description] => 
                [parent] => 0
                [count] => 3
                [filter] => raw

is twice. I used

$jobtypes= array_map("unserialize", array_unique(array_map("serialize", $jobtypes)));

but not working. thanks

2
  • What is the language? Add tag please. Commented May 21, 2014 at 1:29
  • Where does the data come from? If it comes from a database then you should write your query so that duplicates are not included in the result. Also, did you use the search before asking? There are plenty of questions that basically ask the same thing. Commented May 21, 2014 at 1:41

1 Answer 1

2

You can do a manual loop structure looking for the non-existence of each term_id in a unique array, and if it's not there then add it.

In this example I'm using the term_id as the array key for an easy lookup during the loop.

$unique = array();
foreach($your_array as $key => $value) {
    // look for non-existance of term_id in $unique array (as key)
    if(!array_key_exists($value->term_id, $unique)) {
        // add to unique array
        $unique[$value->term_id] = $value;
    }
}

After you've created an array containing unique objects, you can reset those array keys and assign the unique array back to the original array like this:

// reset array keys by assigning values to original array
$your_array = array_values($unique);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.