1

My array looks like this:

Array (

    [1] => stdClass Object
        (
            [id] => 225
            [user_id] => 1
            [name] => Blue Quilted Leather Jacket by Minusey - $499

        )

    [2] => stdClass Object
        (
            [id] => 222
            [user_id] => 1
            [name] => Darling New Bathtub by Duravit - $6300

        )
   [3] => stdClass Object
        (
            [id] => 222
            [user_id] => 1
            [name] => Darling New Bathtub by Duravit - $6300

        ) 

)

I have an array of products that I need to make sure are unique. Need to make this array unique by id. These array are generated by pushing value.

I'm trying to solve this for more than a week now, but I dont get it to work. I know it should be easy...but anyway - I don't get it :D

2
  • You should check in array before pushing if id is present there. Commented Aug 2, 2014 at 5:35
  • Is there anyway to check after pushing and make array unique? Commented Aug 2, 2014 at 5:36

3 Answers 3

3

Can you try this,

$input = array ('1' => array(
            'id' => 225,
            'user_id' => 1,
            'name' => 'Blue Quilted Leather Jacket by Minusey - $499'

        ),
        '2' => array(
                'id' => 222,
                'user_id' => 1,
                'name' => 'Darling New Bathtub by Duravit - $6300'

        ),
        '3' => array(
                'id' => 222,
                'user_id' => 1,
                'name' => 'Darling New Bathtub by Duravit - $6300'

        )                   
  );    

    $UniqueArray = array();
    foreach($input as $key=>$value){   // rebuild your array    
        //$id = $value['id']; //build array with unique key value
        $id = $value->id; //object
        $UniqueArray[$id] = $value;
    }

   print_r($UniqueArray);

output:

   Array
   (
        [225] => Array
        (
                [id] => 225
                [user_id] => 1
                [name] => Blue Quilted Leather Jacket by Minusey - $499
        )

        [222] => Array
        (
                [id] => 222
                [user_id] => 1
                [name] => Darling New Bathtub by Duravit - $6300
        )

   )
Sign up to request clarification or add additional context in comments.

1 Comment

the value in the array are in stdClass Object.
2

try this simple code

$arr_new = array();
$arr_temp_ids = array();
foreach($your_array as $key=>$arr_obj)
{
    $arr_val = get_object_vars($arr_obj);
    if(!isset($arr_temp_ids[$arr_val['id']]))
    {
        $arr_new[] = $arr_obj;
        $arr_temp_ids[$arr_val['id']] = true;
    }
}
var_dump($arr_new); // resultant array

1 Comment

Trying to get property of non-object error in the following line. if(!isset($arr_temp_ids[$arr_val->id]))
2

The basic idea is to loop through the products and save which you encountered. If a product has already been found, just skip it.

<?php
function array_unique_by_key($array, $key = 'id') {
    $found = array(); // Encountered IDs
    $out = array(); // Output array

    foreach ($array as $value) {
        if (!array_key_exists($key, $value)) throw new Exception('Can\'t find key "' . $key . '"');
        $id = $value[$key];

        // If already encountered, skip
        if (in_array($id, $found)) continue;

        // Otherwise, add to found values and to output array
        $found[] = $id;
        $out[] = $value;
    }

    return $out;
}

Usage:

$unique_products = array_unique_by_key($your_products, 'id');

1 Comment

Small syntax error fixed. Algo tested with input data provided, works as expected

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.