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

RhymeGuy
  • 2,102
  • 5
  • 32
  • 62
  • 4
    By default, PHP will try to convert values to strings for comparison. Try specifying "default": `$result = array_unique($array, SORT_REGULAR);` – Aleks G Jul 05 '12 at 08:26

4 Answers4

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);
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 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 Feb 29 '16 at 06:44
  • 1
    @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 https://3v4l.org/DeHIj That's what the manual excerpts try to tell you – KingCrunch Feb 29 '16 at 20:35
  • I see now, that is what the answer is saying but it is not clear to someone who does not understand PHP. – Whitecat Mar 01 '16 at 17:38
10
$uniques = array();
foreach ($array as $obj) {
    $uniques[$obj->product_sku] = $obj;
}

var_dump($uniques);
deceze
  • 510,633
  • 85
  • 743
  • 889
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.

Stegrex
  • 4,004
  • 1
  • 17
  • 19
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;
}
Ajay Gabani
  • 1,168
  • 22
  • 38
Raju Dudhrejiya
  • 1,637
  • 2
  • 15
  • 16