53

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

3550 line is 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

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
        )
)
8
  • 3550 line is array_unique($results_external); Commented Jun 10, 2014 at 9:59
  • 1
    What's the content of $results_external? Commented Jun 10, 2014 at 9:59
  • Please Print_r($results_external) and post Commented Jun 10, 2014 at 10:01
  • i printed pls check it out Commented Jun 10, 2014 at 10:02
  • 4
    @Naruto isssue is solved by addding array_map("unserialize", array_unique(array_map("serialize", $input))); Commented Jun 10, 2014 at 10:09

1 Answer 1

127

As per the docs, array_unique compares 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.

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

Example:

print_r(array_unique($array, SORT_REGULAR));
Sign up to request clarification or add additional context in comments.

1 Comment

after upgrading ( finally ) to php7 there are lots of errors to clear up and this little nugget resolved some of them!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.