仅从此数组中选择唯一的数组值 [英] Select only unique array values from this array

查看:25
本文介绍了仅从此数组中选择唯一的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下变量 $rows:

I have the following variable $rows:

数组(

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

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

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

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

)

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

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

数组(

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

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

)

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

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

可捕获的致命错误:类 stdClass 的对象不能为在 191

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

推荐答案

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

var_dump($uniques);

这篇关于仅从此数组中选择唯一的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆