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

1
  • 4
    By default, PHP will try to convert values to strings for comparison. Try specifying "default": $result = array_unique($array, SORT_REGULAR); Commented Jul 5, 2012 at 8:26

4 Answers 4

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

3 Comments

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.
@Whitecat 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 you
I see now, that is what the answer is saying but it is not clear to someone who does not understand PHP.
10
$uniques = array();
foreach ($array as $obj) {
    $uniques[$obj->product_sku] = $obj;
}

var_dump($uniques);

Comments

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.

1 Comment

Sorry, I was just in the process of writing it... : /
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;
}

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.