php array_unique 显示错误数组到字符串的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24138034/
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 17:10:17  来源:igfitidea点击:

array_unique showing error Array to string conversion

phpcodeignitercodeigniter-url

提问by user3652109

For getting the unique values i am using unique values. Below is the code i am using

为了获得唯一值,我使用了唯一值。下面是我正在使用的代码

array_unique($results_external);
aasort($results_external,"created_on");
$returns_buy_external[]=array_reverse($results_external, true);

If i use the code like this, below is the error i am getting

如果我使用这样的代码,下面是我得到的错误

A PHP Error was encountered Severity: Notice

Message: Array to string conversion

Filename: models/product_model.php

Line Number: 3550

遇到 PHP 错误 严重性:注意

消息:数组到字符串的转换

文件名:models/product_model.php

行号:3550

3550 line is array_unique($results_external);

3550 行是 array_unique($results_external);

Can anyone help me, why it is getting error like this and how to solve it?

谁能帮助我,为什么会出现这样的错误以及如何解决?

results_external sample format coming is below

results_external 样本格式如下

Array
(
    [0] => Array
        (
            [id] => 144
            [name] => test
            [slug] => test
            [images] => {"9eebd0f69772dd3bdf8c787864437c85":{"filename":"9eebd0f69772dd3bdf8c787864437c85.png","alt":"TRESemme Smooth and Shine","caption":""}}
            [track_stock] => 1
            [seo_title] => ttt
            [qty] => 0
            [product_type] => 0
            [price] => 0.00
            [saleprice] => 0.00
            [external_links] => http://test.com
            [external_price] => 285.00
            [external_saleprice] => 285.00
            [created_on] => 2013-11-08 15:03:24
        )
)

回答by Niet the Dark Absol

As per the docs, array_uniquecompares elements as strings by default. This means your 2D array is being converted to an array of strings (all being "Array"and generating the array-to-string Notice) or which only one can be returned as unique.

根据docsarray_unique默认情况下将元素作为字符串进行比较。这意味着您的 2D 数组正在被转换为一个字符串数组(所有这些都是"Array"并生成数组到字符串的通知)或者只有一个可以作为唯一返回。

Use the SORT_REGULARflag to compare the elements as they are, but be aware that arrays are only considered equal if they have the same key-value pairs.

使用该SORT_REGULAR标志按原样比较元素,但请注意,仅当数组具有相同的键值对时,数组才被视为相等。

Example:

例子:

print_r(array_unique($array, SORT_REGULAR));