PHP 5 Error with array_merge()

The error:

  1. warning: array_merge() [function.array-merge]: Argument 2 is not an array


The 411 (as take from PHP.net):

“The behavior of array_merge() was modified in PHP 5. Unlike PHP 4, array_merge() now only accepts parameters of type array. However, you can use typecasting to merge other types.”


Example:

The offending code -

  1. $tag = array_merge($arg1,$arg2);

The fixed code -

  1. $tag = array_merge($arg1,(array)$arg2);

The fix is simple, you cast the offending argument in to hell and wash your hands of it, ok maybe not. But you do cast it just as an array.

You place (array) before the argument that your error says is not an array. In my example you can see the error referred to “argument 2″, so I placed (array) before my second argument.

No all is good and no one gets cast to hell ;-)

This entry was posted on Thursday, March 29th, 2007 at 1:31 pm and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

 

3 Responses to “PHP 5 Error with array_merge()”

  1. Adrienne J. Says:

    OMG thank you so much I was trying to figure out how to fix this for hours today. Thank you thank you thank you.

  2. Michael Noga Says:

    Glad I could help. If you have any other problems you can use my contact for to let me know, I would be happy to try and help out more if I can.

  3. Crow Says:

    Good site - you’re a pretty good writer….. Very creative…

Leave a Reply