Select only unique array values from this array
Asked Answered
S

4

13

I have the following variable $rows:

Array (

[0] => stdClass Object
    (
        [product_sku] => PCH20
    )

[1] => stdClass Object
    (
        [product_sku] => PCH20
    )

[2] => stdClass Object
    (
        [product_sku] => PCH19
    )

[3] => stdClass Object
    (
        [product_sku] => PCH19
    )

)

I need to create second array $second containing only unique values:

Array (

[0] => stdClass Object
    (
        [product_sku] => PCH20
    )

[1] => stdClass Object
    (
        [product_sku] => PCH19
    )

)

But when i run array_unique on $rows, i receive:

Catchable fatal error: Object of class stdClass could not be converted to string on line 191

Scorpius answered 5/7, 2012 at 8:23 Comment(1)
By default, PHP will try to convert values to strings for comparison. Try specifying "default": $result = array_unique($array, SORT_REGULAR);Jaye
Y
10
$uniques = array();
foreach ($array as $obj) {
    $uniques[$obj->product_sku] = $obj;
}

var_dump($uniques);
Yard answered 5/7, 2012 at 8:25 Comment(0)
A
25

array_unique()

The optional second parameter sort_flags may be used to modify the sorting behavior using these values:

Sorting type flags:

  • SORT_REGULAR - compare items normally (don't change types)
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale.

Also note the changenotes below

5.2.10 Changed the default value of sort_flags back to SORT_STRING.

5.2.9 Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.

$values = array_unique($values, SORT_REGULAR);
Almanac answered 5/7, 2012 at 8:26 Comment(3)
It should not be the accepted answer. The question says But when i run array_unique on $rows, i receive: which this question does not address.Cyna
@Cyna SORT_STRING (the default) tries to cast the values to string, before it compares the values and that's where the error comes from. See 3v4l.org/DeHIj That's what the manual excerpts try to tell youAlmanac
I see now, that is what the answer is saying but it is not clear to someone who does not understand PHP.Cyna
Y
10
$uniques = array();
foreach ($array as $obj) {
    $uniques[$obj->product_sku] = $obj;
}

var_dump($uniques);
Yard answered 5/7, 2012 at 8:25 Comment(0)
J
5

The default behavior of function array_unique() is to treat the values inside as strings first. So what's happening is that PHP is attempting to turn your objects into strings (which is throwing the error).

You can modify your function call like this:

$uniqueArray = array_unique($rows, SORT_REGULAR);

This will compare values without changing their data type.

Jael answered 5/7, 2012 at 8:27 Comment(1)
Sorry, I was just in the process of writing it... : /Jael
S
0

Please check below code, I hope this will be helpful to you.

$resultArray = uniqueAssocArray($actualArray, 'product_sku');

function uniqueAssocArray($array, $uniqueKey) 
{
   if (!is_array($array)) 
   {
     return array();
   }
   $uniqueKeys = array();
   foreach ($array as $key => $item) 
   {
     $groupBy=$item[$uniqueKey];
     if (isset( $uniqueKeys[$groupBy]))
     {
        //compare $item with $uniqueKeys[$groupBy] and decide if you 
        //want to use the new item
        $replace= false; 
     }
    else
    {
        $replace=true;
    }
    if ($replace) 
      $uniqueKeys[$groupBy] = $item;   
 }
 return $uniqueKeys;
}
Self answered 10/7, 2015 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.