1

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

4
  • What do you mean by unique? Unique by id? And where that array come from? From db? Commented Aug 1, 2014 at 12:10
  • Wouldn't array_unique work for you? php.net/manual/en/function.array-unique.php Commented Aug 1, 2014 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. Commented Aug 1, 2014 at 12:24
  • Sorry guys, actually i already posted wrong array. this is the correct array to be make unique Commented Aug 1, 2014 at 12:44

5 Answers 5

1

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']; 
}
Sign up to request clarification or add additional context in comments.

Comments

0
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]);
            }
        }
    }
}

Comments

0

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;
    }
}

Comments

0

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,
  ),
)

Comments

0

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);

4 Comments

Actually i already posted wrong array. this is the correct array to be make unique
i have tried to flatten it to 1 level, like you told using call_user_func_array function but nothing happened
Could you provide a JSON dump? It's hard to work a var_dump output
I somehow flatten the array like this:

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.