سؤال

What am I doing wrong with this? If I just run this:

$region = EM_Locations::get(array('orderby'=>'region_name'));

all is good. However when I add in the array_unique:

$region = EM_Locations::get(array('orderby'=>'region_name'));
$reg = array_unique($region)

It breaks and get "EM_Location could not be converted to string"

هل كانت مفيدة؟

المحلول

array_unique() sorts the values treated as string, two elements are considered equal if and only if (string) $elem1 === (string) $elem2.

You can add __toString() method for the EM_Location class.

نصائح أخرى

From the documentation for array_unique:

array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys.

This means that the values must be converted to a string for comparison, but your values appear to be of type EM_Location, which PHP can't figure out how to convert to a string.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top