implode notice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Berrow

    implode notice

    I don't particularly need to do this but I've just found out that

    print implode("<br>", $_SERVER);

    Gives the warning :
    Notice: Array to string conversion in c:\phpdev\www...

    Doesn't do it with any other array and of course

    foreach($_SERVE R as $value){
    print "$value<br> ";
    }

    does exactly the same thing with no warning.

    Curious, I am.

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/
  • Janwillem Borleffs

    #2
    Re: implode notice

    Geoff Berrow wrote:[color=blue]
    > I don't particularly need to do this but I've just found out that
    >
    > print implode("<br>", $_SERVER);
    >
    > Gives the warning :
    > Notice: Array to string conversion in c:\phpdev\www...
    >[/color]

    That's because $_SERVER is an associative array and implode() only works
    with indexed arrays.

    Therefore the following will work:

    print implode("<br>", array_values($_ SERVER));


    JW



    Comment

    • Hartmut Holzgraefe

      #3
      Re: implode notice

      Janwillem Borleffs wrote:[color=blue]
      > That's because $_SERVER is an associative array and implode() only works
      > with indexed arrays.[/color]

      This is *not* true, implode() doesn't care about the array type at
      all, it just joins the array contents in its current sort order
      without looking at the index values at all.

      --
      Hartmut Holzgraefe, Senior Support Engineer .
      MySQL AB, www.mysql.com

      Are you MySQL certified? www.mysql.com/certification

      Comment

      • Hartmut Holzgraefe

        #4
        Re: implode notice

        Geoff Berrow wrote:[color=blue]
        > I don't particularly need to do this but I've just found out that
        >
        > print implode("<br>", $_SERVER);
        >
        > Gives the warning :
        > Notice: Array to string conversion in c:\phpdev\www...
        >[/color]

        $_SERVER["argv"] is also an array, that's what is causing the
        warning
        [color=blue]
        > Doesn't do it with any other array and of course[/color]

        You just have to construct the right example and it does:

        <?php
        error_reporting (E_ALL);
        $a = array(1,2, array(3,4));
        implode("-",$a);
        ?>
        [color=blue]
        > foreach($_SERVE R as $value){
        > print "$value<br> ";
        > }
        >
        > does exactly the same thing with no warning.[/color]

        The "Array to String" conversion warning ist not thrown in all
        situations where such a conversion happens

        --
        Hartmut Holzgraefe, Senior Support Engineer .
        MySQL AB, www.mysql.com

        Are you MySQL certified? www.mysql.com/certification

        Comment

        • Janwillem Borleffs

          #5
          Re: implode notice

          Hartmut Holzgraefe wrote:[color=blue]
          > This is *not* true, implode() doesn't care about the array type at
          > all, it just joins the array contents in its current sort order
          > without looking at the index values at all.[/color]

          I stand corrected...


          JW



          Comment

          • Janwillem Borleffs

            #6
            Re: implode notice

            Hartmut Holzgraefe wrote:[color=blue]
            > The "Array to String" conversion warning ist not thrown in all
            > situations where such a conversion happens[/color]

            Appears to happen only when explicit casting is performed:

            // Does not throw a notice:
            print array(1,2);

            // Does throw a notice:
            print (string) array(1,2);


            JW



            Comment

            Working...