php 仅从此数组中选择唯一的数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11340450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:18:44  来源:igfitidea点击:

Select only unique array values from this array

phparraysarray-unique

提问by RhymeGuy

I have the following variable $rows:

我有以下变量 $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
    )

)

大批 (

[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:

我需要创建仅包含唯一值的第二个数组 $second :

Array (

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

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

)

大批 (

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

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

)

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

但是当我在 $rows 上运行 array_unique 时,我收到:

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

可捕获的致命错误:无法在第191行将类 stdClass 的对象转换为字符串

采纳答案by deceze

$uniques = array();
foreach ($array as $obj) {
    $uniques[$obj->product_sku] = $obj;
}

var_dump($uniques);

回答by KingCrunch

array_unique()

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.

可选的第二个参数 sort_flags 可用于使用以下值修改排序行为:

排序类型标志:

  • SORT_REGULAR - 正常比较项目(不要改变类型)
  • SORT_NUMERIC - 以数字方式比较项目
  • SORT_STRING - 将项目作为字符串进行比较
  • SORT_LOCALE_STRING - 根据当前语言环境将项目作为字符串进行比较。

Also note the changenotes below

另请注意下面的更改说明

5.2.10Changed the default value of sort_flags back to SORT_STRING.

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

5.2.10将 sort_flags 的默认值改回 SORT_STRING。

5.2.9添加了可选的 sort_flags,默认为 SORT_REGULAR。在 5.2.9 之前,该函数用于在内部使用 SORT_STRING 对数组进行排序。

$values = array_unique($values, SORT_REGULAR);

回答by Stegrex

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

函数的默认行为array_unique()是首先将内部的值视为字符串。所以发生的事情是 PHP 试图将您的对象转换为字符串(这会引发错误)。

You can modify your function call like this:

您可以像这样修改您的函数调用:

$uniqueArray = array_unique($rows, SORT_REGULAR);

This will compare values without changing their data type.

这将比较值而不更改它们的数据类型。

回答by Raju Dudhrejiya

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