Wednesday 15 April 2015

PHP in_array object comparing? -


What can the in_array function compare to objects?

For example, I have an array of objects and I want to add them explicitly to another array. Is it possible to check that an object has already been added:

  in_array ($ different, $ object);   

Or is there any other way?

The function can not compare objects.

You should make a unique key-value pair from your object and only compare those keys when inserting a new object into your final array.

P> Assume that each object has a unique id property, possible solution will be:

  $ unique_objects = array (); // $ data represents your object collection foreach ($ data as $ item) (if! (Array_key_exists ($ item-> ID, unique_object)) {$ unique_objects [$ idem- & gt; ID] = $ Obj;}}    

No comments:

Post a Comment