Wednesday, March 30, 2011

implode() notice: php array to string conversion

the general format of the implode() function are as follows:
i)    string implode ( $separator, $array)
ii)    string implode (  $array)
iii)    string implode ( $array, $separator)
I faced a little problem when “$array” wasn’t just an indexed array
but an associative array (thus array of arrays).
I posted this so I can help someone save 10mins ;)
Solution:
if you have an associative array say $array,
use array_values() as shown in the example below:
implode ( ‘,’, array_values($array))
hope it helps!
S

19 comments:

  1. Thanks, u saved me

    ReplyDelete
  2. it is exactly what I am looking for :) Thank you

    ReplyDelete
  3. Might be worth pointing out that according to php.net in php5.4:
    "Converting an array to a string will now generate an E_NOTICE level error, but the result of the cast will still be the string "Array"."

    So if you are updating your PHP from 5.3 to 5.4 don't be surprised to see this fix not working. Just getting through debugging around 50 Notices myself now. :)

    ReplyDelete
  4. Thanks. Your explanation solved my problem!

    ReplyDelete
  5. Thank you, you did help me with this !

    ReplyDelete
  6. It isn't the same thing, but if all you are trying to do is serialize an associative array, consider using json_encode instead.

    ReplyDelete
  7. It still gives me the notice even after using the array_values($array).
    Any help would be appreciated .

    ReplyDelete
  8. Me, too. I still get the array to string conversion error.

    ReplyDelete
  9. you also get this error when you make a ugly typo.. ;-(

    implode ( ‘,’. array_values($array))

    when you do a "." instead of a "," ;-)

    ReplyDelete
  10. 888sport Review of the online gaming platform in 2021
    888sport, 블랙 잭 만화 an online gaming platform 넷마블바카라 that offers top 브라 벗기기 casino games from bet 분석 around the globe, launched in 2004. The site is powered by 헐리우드 노출 NetEnt's

    ReplyDelete