Hugo Mota
Hugo Mota

Reputation: 11577

array_diff() failing while filtering object arrays

I'm trying to "diff" two arrays and PHP is returning the following error:

Catchable fatal error: Object of class Node could not be converted to string in D:\projetos\labs\dok\dev\application\models\dok.php on line 410

my arrays are, respectively:

array(2)
    object(Node)#5 ...[7 attributes]
    object(Node)#6 ...[7 attributes]

array(1)
    object(Node)#5 ....[7 attributes]

I would really appreciate some help here as I have no clue of what's going on.


as requested, the code around the diff:

// is it a distant child?
$all_childs = $node->get_childs(true);
$distant_childs = array_diff($all_childs, $childs);
if(in_array($this, $distant_childs))
    return 'distant-child';

Upvotes: 2

Views: 1247

Answers (1)

xdazz
xdazz

Reputation: 160883

Note:

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

If the element is object, you need to provide __toString() method for the object, or you did it by yourself.

Upvotes: 2

Related Questions