Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this array

Array (

[0] => Array
    (
        [0] => stdClass Object
            (
                [id] => 226
                [user_id] => 1
                [name] => Eden Corner Tub by Glass - $2099

            )

        [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

            )



    )

[1] => Array
    (
        [0] => stdClass Object
            (
                [id] => 226
                [user_id] => 1
                [name] => Eden Corner Tub by Glass - $2099
            )

        [1] => stdClass Object
            (
                [id] => 229
                [user_id] => 1
                [name] => Batman Tumbler Golf Cart - $50000

            )

        [2] => stdClass Object
            (
                [id] => 228
                [user_id] => 1
                [name] => Swirlio Frozen Fruit Dessert Maker - $60

            )

    )    )

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

share|improve this question
    
What do you mean by unique? Unique by id? And where that array come from? From db? –  baldrs Aug 1 at 12:10
    
Wouldn't array_unique work for you? php.net/manual/en/function.array-unique.php –  Ronni Egeriis Aug 1 at 12:16
    
@RonniEgeriis no, it won't, because elements are considered unique by their string representation, thus array of arrays won't be uqinue'd correctly. (string) array() is Array + notice. –  baldrs Aug 1 at 12:24
    
Sorry guys, actually i already posted wrong array. this is the correct array to be make unique –  user3572716 Aug 1 at 12:44

5 Answers 5

Try this:

$array = array(

    0 =>  array(  
        "name"   => "test",  
        "id"    =>  4  
    ),  
    1   =>  array(  
        "name" =>  "test2",  
        "id" => 152  
    ), 
    2   =>  array(  
        "name" =>  "test2",  
        "id" => 152  
    )
 );

$newArray = array();
foreach($array as $value) {
    $newArray[$value['id']]['name'] = $value['name'];
    $newArray[$value['id']]['id'] = $value['id']; 
}
share|improve this answer
foreach($array as $key=>$inner_array)
{
    foreach($array as $key_again=>$array_again)
    {
        if($key != $key_again)
        {
            if($inner_array['name'] != $array_again['name'] AND $inner_array['id'] != $array_again['id'])
            {
                //its okay
            }
            else
            {
                unset($array[$key]);
            }
        }
    }
}
share|improve this answer

Not sure how the arrays are generated, but, if you can change that, you could set the array keys to the IDs directly and check if the id is already set.

Otherwise, you can do the following:

$unique = array();

foreach( $array as $values ) {
    if( ! isset( $unique[$values['id']] ) ) {
        $unique[$values['id']] = $values;
    }
}
share|improve this answer

This will make your array unique:

$array = array(
  0 =>  array(  
    "name"   => "test",  
    "id"    =>  4  
  ),  
  1   =>  array(  
    "name" =>  "test2",  
    "id" => 152  
  ), 
  2   =>  array(  
    "name" =>  "test2",  
    "id" => 152  
  ) );

$result = array();

$index = array();

foreach($array as $i => $elem) {
  if(!isset($index[$elem['id']])) {
    $result[$i] = $elem;

    $index[$elem['id']] = 1;
  }
}

echo var_export($result);

Output:

array (
  0 => 
  array (
    'name' => 'test',
    'id' => 4,
  ),
  1 => 
  array (
    'name' => 'test2',
    'id' => 152,
  ),
)
share|improve this answer

This will work. It could be considered more clean than a for loop, but I'm not sure about performance.

$array = [
    [ "name" => "test", "id" => 4 ],
    [ "name" => "test2", "id" => 152 ], 
    [ "name" => "test2", "id" => 152 ]
];

array_walk($array, function(&$item, $idx) use($array){
    $matches = array_slice(array_keys($array, $item), 1);
    if (in_array($idx, $matches)) {
        $item = false;
    }
});
$array = array_filter($array);

Edit Since you updated the data set to work with, you would need to flatten it 1 level:

$array = call_user_func_array('array_merge', $array);
share|improve this answer
    
Sorry Ronni, this doesn't work –  user3572716 Aug 1 at 12:51
    
@user3572716 Well, you updated your data :-) –  Ronni Egeriis Aug 1 at 12:51
    
Actually i already posted wrong array. this is the correct array to be make unique –  user3572716 Aug 1 at 12:55
    
@user3572716 I have updated my answer. –  Ronni Egeriis Aug 1 at 12:57
    
i have tried to flatten it to 1 level, like you told using call_user_func_array function but nothing happened –  user3572716 Aug 1 at 12:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.