-1

I have two arrays which I merge using:

$query = array_merge($query1, $query2);

After merging I want to apply array unique:

$query = array_unique($query);

For some reason it gives me a php error, a page black.

I suspect it's because of the structure of the array's that need merging.

A vardump illustrates the structure of query1 and query 2:

array(73) { 
    [0]=> object(stdClass)#32 (6) { 
        ["pic0"]=> string(78) "the picture1 link" 
        ["bio"]=> string(22)  "the bio1" 
    } 
    [1]=> object(stdClass)#96 (6) { 
        ["pic0"]=> string(70) "the picture2 link" 
        ["bio"]=> string(225) "the bio2" 
    } 
 }
8
  • 3
    "For some reason it gives me a php error" --- is it that game when we need to guess the actual error message? "I suspect" --- the fact is - programming is a precise thing, we don't care of suspects, we care of facts. Commented Aug 26, 2012 at 21:46
  • So what error did it give you? A blank page is hardly an error. It usually hints to an error somewhere... Commented Aug 26, 2012 at 21:46
  • Yes a black page, let me try editing the question. So you are saying the array structure is ok for the array_unique function to work? So i should look elsewhere? Commented Aug 26, 2012 at 21:47
  • Wait... a black page? Not a blank page? I'm confused... Commented Aug 26, 2012 at 21:47
  • Black page? :-S Are you sure your monitor is turned on? PS: have you checked error logs? Commented Aug 26, 2012 at 21:47

1 Answer 1

1

array_unique works by comparing the elements as strings. Objects generally can't be converted to strings, so you are getting an error.

Try using arrays instead of stdClasses, and also make sure you set the SORT_REGULAR flag.

Sign up to request clarification or add additional context in comments.

3 Comments

Ty very much, got it :) - but I am afraid I cannot do this because $query is a mysql query result using wordpress get_results() which returns stdClasses. $query = $wpdb->get_results(mysql query);
I merged two arrays containing query results and I wanted to eliminate the uniques
Try adding the SORT_REGULAR flag and see if that works. I don't know how object comparison works (or if it works at all), but it's worth a shot.

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.