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 )
)
)
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 )
)
)
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
可捕获的致命错误:类stdClass的对象无法在第191行转换为字符串
4 个解决方案
#1
10
$uniques = array();
foreach ($array as $obj) {
$uniques[$obj->product_sku] = $obj;
}
var_dump($uniques);
#2
24
array_unique()
The optional second parameter sort_flags may be used to modify the sorting behavior using these values:
可选的第二个参数sort_flags可用于使用以下值修改排序行为:
Sorting type flags:
排序类型标志:
- SORT_REGULAR - compare items normally (don't change types)
- SORT_REGULAR - 正常比较项目(不要更改类型)
- SORT_NUMERIC - compare items numerically
- SORT_NUMERIC - 以数字方式比较项目
- SORT_STRING - compare items as strings
- SORT_STRING - 将项目作为字符串进行比较
- SORT_LOCALE_STRING - compare items as strings, based on the current locale.
- SORT_LOCALE_STRING - 根据当前区域设置将项目作为字符串进行比较。
Also note the changenotes below
另请注意下面的变更说明
5.2.10 Changed the default value of sort_flags back to SORT_STRING.
5.2.10将sort_flags的默认值更改回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.
5.2.9添加了可选的sort_flags默认为SORT_REGULAR。在5.2.9之前,此函数用于在内部对SORT_STRING对数组进行排序。
$values = array_unique($values, SORT_REGULAR);
#3
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).
函数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.
这将比较值而不更改其数据类型。
#4
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;
}
#1
10
$uniques = array();
foreach ($array as $obj) {
$uniques[$obj->product_sku] = $obj;
}
var_dump($uniques);
#2
24
array_unique()
The optional second parameter sort_flags may be used to modify the sorting behavior using these values:
可选的第二个参数sort_flags可用于使用以下值修改排序行为:
Sorting type flags:
排序类型标志:
- SORT_REGULAR - compare items normally (don't change types)
- SORT_REGULAR - 正常比较项目(不要更改类型)
- SORT_NUMERIC - compare items numerically
- SORT_NUMERIC - 以数字方式比较项目
- SORT_STRING - compare items as strings
- SORT_STRING - 将项目作为字符串进行比较
- SORT_LOCALE_STRING - compare items as strings, based on the current locale.
- SORT_LOCALE_STRING - 根据当前区域设置将项目作为字符串进行比较。
Also note the changenotes below
另请注意下面的变更说明
5.2.10 Changed the default value of sort_flags back to SORT_STRING.
5.2.10将sort_flags的默认值更改回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.
5.2.9添加了可选的sort_flags默认为SORT_REGULAR。在5.2.9之前,此函数用于在内部对SORT_STRING对数组进行排序。
$values = array_unique($values, SORT_REGULAR);
#3
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).
函数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.
这将比较值而不更改其数据类型。
#4
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;
}