"Array to String Conversion" error when constructing a multi-dimensional array

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

    "Array to String Conversion" error when constructing a multi-dimensional array

    Hi,

    I'm a bit stumped as I am getting a "Notice: Array to String
    Conversion" error when trying to do something that on the surface
    should be a very simple task - create an array, and write a set of
    values to them based on data submitted from POST Fields.

    Code below:

    $_SESSION["increment"] = array();
    $x = 0 // Counter - This will be
    incremented

    $_SESSION["increment"][$x] = array("incremen t_bond" =>
    $_POST["increment_bond "],
    "increment_amou nt" = $_POST["increment_amou nt"],
    "increment_comm _date1" = $_POST["increment_comm _date1"],
    "increment_comm _date2" =$_POST["increment_comm _date2"],
    "increment_comm _date3" = $_POST["increment_comm _date3"]);


    It keeps throwing out that error, and does not write any of the POST
    Values to the array (the POST values are definitely getting sent, so
    that is not the problem).

    If I dump out the value of $_SESSION["increment"], I get "1A" ?!?

    I'm sure that there is a pretty simple mistake lurking somewhere - I
    just can't seem to figure it out.

    Any suggestions or hints ?

    Thanks
    Neil.

  • Erwin Moller

    #2
    Re: "Array to String Conversion&quot ; error when constructing a multi-dimensional array

    drako wrote:
    Hi,
    >
    I'm a bit stumped as I am getting a "Notice: Array to String
    Conversion" error when trying to do something that on the surface
    should be a very simple task - create an array, and write a set of
    values to them based on data submitted from POST Fields.
    >
    Code below:
    >
    $_SESSION["increment"] = array();
    $x = 0 // Counter - This will be
    incremented
    >
    $_SESSION["increment"][$x] = array("incremen t_bond" =>
    $_POST["increment_bond "],
    "increment_amou nt" = $_POST["increment_amou nt"],
    "increment_comm _date1" = $_POST["increment_comm _date1"],
    "increment_comm _date2" =$_POST["increment_comm _date2"],
    "increment_comm _date3" = $_POST["increment_comm _date3"]);
    >
    >
    It keeps throwing out that error, and does not write any of the POST
    Values to the array (the POST values are definitely getting sent, so
    that is not the problem).
    >
    If I dump out the value of $_SESSION["increment"], I get "1A" ?!?
    >
    I'm sure that there is a pretty simple mistake lurking somewhere - I
    just can't seem to figure it out.
    >
    Any suggestions or hints ?
    >
    Thanks
    Neil.
    Hi Neil,

    Your code looks perfectly OK to me.
    Are you 100% sure this is where the error is thrown?
    Did you try:
    <pre>
    <?php print_r($_POST) ; ?>
    </pre>

    Does it produce all used postvars you expect?

    Regards,
    Erwin Moller

    Comment

    • drako

      #3
      Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

      Erwin,

      I have tried <?php print_r($_POST) ; ?>, and it outputs all the POST
      vars that were submitted. So now problem here.

      And the error is thrown at the line that begins
      "$_SESSION["increment"][$x] ........"

      I have tried this code on two different systems (5.1.6 on OSX, and
      5.2.0 on NetBSD), and the same error keeps occuring.

      Very puzzling.

      Neil.


      Erwin Moller wrote:
      drako wrote:
      >
      Hi,

      I'm a bit stumped as I am getting a "Notice: Array to String
      Conversion" error when trying to do something that on the surface
      should be a very simple task - create an array, and write a set of
      values to them based on data submitted from POST Fields.

      Code below:

      $_SESSION["increment"] = array();
      $x = 0 // Counter - This will be
      incremented

      $_SESSION["increment"][$x] = array("incremen t_bond" =>
      $_POST["increment_bond "],
      "increment_amou nt" = $_POST["increment_amou nt"],
      "increment_comm _date1" = $_POST["increment_comm _date1"],
      "increment_comm _date2" =$_POST["increment_comm _date2"],
      "increment_comm _date3" = $_POST["increment_comm _date3"]);


      It keeps throwing out that error, and does not write any of the POST
      Values to the array (the POST values are definitely getting sent, so
      that is not the problem).

      If I dump out the value of $_SESSION["increment"], I get "1A" ?!?

      I'm sure that there is a pretty simple mistake lurking somewhere - I
      just can't seem to figure it out.

      Any suggestions or hints ?

      Thanks
      Neil.
      >
      Hi Neil,
      >
      Your code looks perfectly OK to me.
      Are you 100% sure this is where the error is thrown?
      Did you try:
      <pre>
      <?php print_r($_POST) ; ?>
      </pre>
      >
      Does it produce all used postvars you expect?
      >
      Regards,
      Erwin Moller

      Comment

      • Heiko Richler

        #4
        Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensionalarra y

        drako wrote:
        Hi,
        >
        I'm a bit stumped as I am getting a "Notice: Array to String
        Conversion" error when trying to do something that on the surface
        should be a very simple task - create an array, and write a set of
        values to them based on data submitted from POST Fields.
        >
        Code below:
        >
        $_SESSION["increment"] = array();
        Try doing <?php print_r($_SESSI ON["increment"]); ?here ... (*)
        $x = 0 // Counter - This will be
        incremented
        (*) do you have any code in here?
        What does <?php print_r($increm ent); ?tell you?

        (*) ... and an other <?php print_r($_SESSI ON["increment"]); ?here.
        $_SESSION["increment"][$x] = array("incremen t_bond" =>
        $_POST["increment_bond "],
        "increment_amou nt" = $_POST["increment_amou nt"],
        "increment_comm _date1" = $_POST["increment_comm _date1"],
        "increment_comm _date2" =$_POST["increment_comm _date2"],
        "increment_comm _date3" = $_POST["increment_comm _date3"]);
        Heiko
        --
        http://portal.richler.de/ Namensportal zu Richler
        http://www.richler.de/ Heiko Richler: Computer - Know How!
        http://www.richler.info/ private Homepage

        Comment

        • drako

          #5
          Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

          OK,

          I inserted the debugging statements as you suggested:


          $_SESSION["increment"] = array();
          echo '1: ';print_r($_SES SION["increment"]);echo '<br>';

          $x = 0 // Counter - This will be
          incremented
          echo '2: ';print_r($incr ement); echo '<br>';


          $_SESSION["increment"][$x] = array("incremen t_bond" =>
          $_POST["increment_bond "],
          "increment_amou nt" = $_POST["increment_amou nt"],
          "increment_comm _date1" =>
          $_POST["increment_comm _date1"],
          "increment_comm _date2" =>
          $_POST["increment_comm _date2"],
          "increment_comm _date3" =>
          $_POST["increment_comm _date3"]);

          echo '3: ';print_r($_SES SION["increment"]);exit;

          <------->
          The output is:

          1: 1
          2:
          3: 1A


          I'm still no wiser as to what is going on here....

          Rgds
          Neil.


          Heiko Richler wrote:
          drako wrote:
          Hi,

          I'm a bit stumped as I am getting a "Notice: Array to String
          Conversion" error when trying to do something that on the surface
          should be a very simple task - create an array, and write a set of
          values to them based on data submitted from POST Fields.

          Code below:

          $_SESSION["increment"] = array();
          >
          Try doing <?php print_r($_SESSI ON["increment"]); ?here ... (*)
          >
          $x = 0 // Counter - This will be
          incremented
          >
          (*) do you have any code in here?
          What does <?php print_r($increm ent); ?tell you?
          >
          (*) ... and an other <?php print_r($_SESSI ON["increment"]); ?here.
          >
          $_SESSION["increment"][$x] = array("incremen t_bond" =>
          $_POST["increment_bond "],
          "increment_amou nt" = $_POST["increment_amou nt"],
          "increment_comm _date1" = $_POST["increment_comm _date1"],
          "increment_comm _date2" =$_POST["increment_comm _date2"],
          "increment_comm _date3" = $_POST["increment_comm _date3"]);
          >
          Heiko
          --
          http://portal.richler.de/ Namensportal zu Richler
          http://www.richler.de/ Heiko Richler: Computer - Know How!
          http://www.richler.info/ private Homepage

          Comment

          • Kimmo Laine

            #6
            Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

            "drako" <neil@invidion. co.ukwrote in message
            news:1169067038 .010143.224210@ a75g2000cwd.goo glegroups.com.. .
            OK,
            >
            I inserted the debugging statements as you suggested:
            >
            >
            $_SESSION["increment"] = array();
            echo '1: ';print_r($_SES SION["increment"]);echo '<br>';
            >
            $x = 0 // Counter - This will be
            incremented
            echo '2: ';print_r($incr ement); echo '<br>';
            I don't understand what this $increment variable has to do with anything.

            --
            "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
            http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
            spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


            Comment

            • drako

              #7
              Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array



              Quote:
              I don't understand what this $increment variable has to do with anything.
              It doesn't actually relate to anything; but I suspect it was suggested
              just to see if anything was output - and nothing was output.

              I know if 'register_globa ls' is switched on, this could potentially
              cause a problem, but in this case, globals is switched off, so is not
              the problem.

              I now suspect this is session-related in someway. As the code looks OK,
              the problem arises from elsewhere....

              Kimmo Laine wrote:
              "drako" <neil@invidion. co.ukwrote in message
              news:1169067038 .010143.224210@ a75g2000cwd.goo glegroups.com.. .
              OK,

              I inserted the debugging statements as you suggested:


              $_SESSION["increment"] = array();
              echo '1: ';print_r($_SES SION["increment"]);echo '<br>';

              $x = 0 // Counter - This will be
              incremented
              echo '2: ';print_r($incr ement); echo '<br>';
              >
              I don't understand what this $increment variable has to do with anything.
              >
              --
              "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
              http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
              spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)

              Comment

              • Rik

                #8
                Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

                drako wrote:


                Don't fix bottom-posting, fix topposting.....
                Kimmo Laine wrote:
                >"drako" <neil@invidion. co.ukwrote in message
                >news:116906703 8.010143.224210 @a75g2000cwd.go oglegroups.com. ..
                >>OK,
                >>>
                >>I inserted the debugging statements as you suggested:
                >>>
                >>>
                >> $_SESSION["increment"] = array();
                >>echo '1: ';print_r($_SES SION["increment"]);echo '<br>';
                >>>
                >> $x = 0 // Counter - This will be
                >>incremented
                >>echo '2: ';print_r($incr ement); echo '<br>';
                >>
                >I don't understand what this $increment variable has to do with
                >anything.
                >
                It doesn't actually relate to anything; but I suspect it was
                suggested just to see if anything was output - and nothing was output.
                >
                I know if 'register_globa ls' is switched on, this could potentially
                cause a problem, but in this case, globals is switched off, so is not
                the problem.
                >
                I now suspect this is session-related in someway. As the code looks
                OK, the problem arises from elsewhere....
                Nope.
                Keep in mind, that on strings, the [] will get or set the character of the
                string at that particular location. My suspicion is that you don't give us
                the real code (for where would the incrementation take place?), and you
                turn the array into the string ( or number) '1' somewhere. (This often
                happens when you use the return of a function (true) that works be a
                reference instead of a return by accident.)

                So, in the first loop, we've made $_SESSION["increment"] into a string by
                accident. On the second loop, if $x = 1, that's the second position in the
                string (which was not previously set). You try to set a character to an
                array, so the array is converted to a string ('Array'), then it takes the
                first character ('A') from that string, and put it at position 1. Hence
                resulting in '1A'.

                If you gave us the real code, I'd bet we'd spot where the actual overriding
                on $_SESSION["increment"] takes place.
                --
                Rik Wasmus


                Comment

                • Erwin Moller

                  #9
                  Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

                  Rik wrote:
                  drako wrote:
                  >
                  >
                  Don't fix bottom-posting, fix topposting.....
                  >
                  >Kimmo Laine wrote:
                  >>"drako" <neil@invidion. co.ukwrote in message
                  >>news:11690670 38.010143.22421 0@a75g2000cwd.g ooglegroups.com ...
                  >>>OK,
                  >>>>
                  >>>I inserted the debugging statements as you suggested:
                  >>>>
                  >>>>
                  >>> $_SESSION["increment"] = array();
                  >>>echo '1: ';print_r($_SES SION["increment"]);echo '<br>';
                  >>>>
                  >>> $x = 0 // Counter - This will be
                  >>>incremente d
                  >>>echo '2: ';print_r($incr ement); echo '<br>';
                  >>>
                  >>I don't understand what this $increment variable has to do with
                  >>anything.
                  >>
                  >It doesn't actually relate to anything; but I suspect it was
                  >suggested just to see if anything was output - and nothing was output.
                  >>
                  >I know if 'register_globa ls' is switched on, this could potentially
                  >cause a problem, but in this case, globals is switched off, so is not
                  >the problem.
                  >>
                  >I now suspect this is session-related in someway. As the code looks
                  >OK, the problem arises from elsewhere....
                  >
                  Nope.
                  Keep in mind, that on strings, the [] will get or set the character of the
                  string at that particular location. My suspicion is that you don't give us
                  the real code (for where would the incrementation take place?), and you
                  turn the array into the string ( or number) '1' somewhere. (This often
                  happens when you use the return of a function (true) that works be a
                  reference instead of a return by accident.)
                  >
                  So, in the first loop, we've made $_SESSION["increment"] into a string by
                  accident. On the second loop, if $x = 1, that's the second position in
                  the string (which was not previously set). You try to set a character to
                  an array, so the array is converted to a string ('Array'), then it takes
                  the
                  first character ('A') from that string, and put it at position 1. Hence
                  resulting in '1A'.
                  >
                  If you gave us the real code, I'd bet we'd spot where the actual
                  overriding on $_SESSION["increment"] takes place.
                  Damn Rik, sometimes you are scaringly smart.
                  That sounds like a very probable scenario you described and deducted from
                  the sparse information given.
                  Respect!

                  Erwin Moller

                  Comment

                  • Toby Inkster

                    #10
                    Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

                    drako wrote:
                    I'm sure that there is a pretty simple mistake lurking somewhere - I
                    just can't seem to figure it out.
                    >
                    Any suggestions or hints ?
                    There doesn't seem to be anything wrong with the code you posted (except
                    for a missing semicolon after $x = 0), so I suspect the problem lies
                    somewhere in your other code.

                    Because of this missing semicolon, I suspect you have chopped out a
                    section of code between '$x=0' and '$_SESSION["increment"][$x]=array(...)'.
                    That is almost certainly where your problem lies. In particular, I think
                    you may have assigned some non-integer value to $x at some point.

                    Do 'print_r($x);' just before the error happens and see what you see.

                    --
                    Toby A Inkster BSc (Hons) ARCS
                    Contact Me ~ http://tobyinkster.co.uk/contact

                    Comment

                    • drako

                      #11
                      Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

                      You are correct in assuming that I chopped out a line when I posted
                      this...

                      The missing line between '$x=0' and
                      '$_SESSION["increment"][$x]=array(...)'.

                      is

                      if(isset($_SESS ION["increments "])) {$x =
                      count($_SESSION["increments "]);}

                      The purpose is to count the number of entries in the array - if count
                      returns '5', then the last value of $x would have been 4, so we can
                      assign '5' as the next value in the array.

                      Could this be the offending line...



                      Toby Inkster wrote:
                      drako wrote:
                      >
                      I'm sure that there is a pretty simple mistake lurking somewhere - I
                      just can't seem to figure it out.

                      Any suggestions or hints ?
                      >
                      There doesn't seem to be anything wrong with the code you posted (except
                      for a missing semicolon after $x = 0), so I suspect the problem lies
                      somewhere in your other code.
                      >
                      Because of this missing semicolon, I suspect you have chopped out a
                      section of code between '$x=0' and '$_SESSION["increment"][$x]=array(...)'.
                      That is almost certainly where your problem lies. In particular, I think
                      you may have assigned some non-integer value to $x at some point.
                      >
                      Do 'print_r($x);' just before the error happens and see what you see.
                      >
                      --
                      Toby A Inkster BSc (Hons) ARCS
                      Contact Me ~ http://tobyinkster.co.uk/contact

                      Comment

                      • drako

                        #12
                        Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array


                        Sorry, the extra line of code above should have read:

                        if(isset($_SESS ION["increment"])) {$x =
                        count($_SESSION["increment"]);}

                        I just noticed the typo, in case anyone has been sharp enough to spot
                        it...


                        drako wrote:
                        You are correct in assuming that I chopped out a line when I posted
                        this...
                        >
                        The missing line between '$x=0' and
                        '$_SESSION["increment"][$x]=array(...)'.
                        >
                        is
                        >
                        if(isset($_SESS ION["increments "])) {$x =
                        count($_SESSION["increments "]);}
                        >
                        The purpose is to count the number of entries in the array - if count
                        returns '5', then the last value of $x would have been 4, so we can
                        assign '5' as the next value in the array.
                        >
                        Could this be the offending line...
                        >
                        >
                        >
                        Toby Inkster wrote:
                        drako wrote:
                        I'm sure that there is a pretty simple mistake lurking somewhere - I
                        just can't seem to figure it out.
                        >
                        Any suggestions or hints ?
                        There doesn't seem to be anything wrong with the code you posted (except
                        for a missing semicolon after $x = 0), so I suspect the problem lies
                        somewhere in your other code.

                        Because of this missing semicolon, I suspect you have chopped out a
                        section of code between '$x=0' and '$_SESSION["increment"][$x]=array(...)'.
                        That is almost certainly where your problem lies. In particular, I think
                        you may have assigned some non-integer value to $x at some point.

                        Do 'print_r($x);' just before the error happens and see what you see.

                        --
                        Toby A Inkster BSc (Hons) ARCS
                        Contact Me ~ http://tobyinkster.co.uk/contact

                        Comment

                        • Heiko Richler

                          #13
                          Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensionalarra y

                          drako wrote:
                          Sorry, the extra line of code above should have read:
                          >
                          if(isset($_SESS ION["increment"])) {$x =
                          count($_SESSION["increment"]);}
                          Do you know you do not need $x?

                          See "Creating/modifying with square-bracket syntax" at


                          Heiko
                          --
                          http://portal.richler.de/ Namensportal zu Richler
                          http://www.richler.de/ Heiko Richler: Computer - Know How!
                          http://www.richler.info/ private Homepage

                          Comment

                          • Rik

                            #14
                            Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

                            drako wrote:
                            drako wrote:
                            >You are correct in assuming that I chopped out a line when I posted
                            >this...
                            >>
                            >The missing line between '$x=0' and
                            >'$_SESSION["increment"][$x]=array(...)'.
                            >>
                            >is
                            >>
                            >if(isset($_SES SION["increments "])) {$x =
                            >count($_SESSIO N["increments "]);}
                            >>
                            >The purpose is to count the number of entries in the array - if count
                            >returns '5', then the last value of $x would have been 4, so we can
                            >assign '5' as the next value in the array.
                            >>
                            >Could this be the offending line...
                            Sorry, the extra line of code above should have read:
                            >
                            if(isset($_SESS ION["increment"])) {$x =
                            count($_SESSION["increment"]);}
                            >
                            I just noticed the typo, in case anyone has been sharp enough to spot
                            it...
                            But that's not it afaik. See the comment about just using empty [], this
                            will just add an array-value to the end.

                            However, there's something else going on in the adding. Posting the total
                            code that works on $_SESSION["increment"] woudl speed up the process :-).

                            You probably have a $_SESSION["increment"] = something(); somehere, where
                            the function just returns true instead of a processed array, casting it to
                            the '1' string.
                            --
                            Rik Wasmus


                            Comment

                            • Rik

                              #15
                              Re: &quot;Array to String Conversion&quot ; error when constructing a multi-dimensional array

                              Erwin Moller wrote:
                              Rik wrote:
                              >>I now suspect this is session-related in someway. As the code looks
                              >>OK, the problem arises from elsewhere....
                              >>
                              >Nope.
                              >Keep in mind, that on strings, the [] will get or set the character
                              >of the string at that particular location. My suspicion is that you
                              >don't give us the real code (for where would the incrementation take
                              >place?), and you turn the array into the string ( or number) '1'
                              >somewhere. (This often happens when you use the return of a function
                              >(true) that works be a reference instead of a return by accident.)
                              >>
                              >So, in the first loop, we've made $_SESSION["increment"] into a
                              >string by accident. On the second loop, if $x = 1, that's the
                              >second position in the string (which was not previously set). You
                              >try to set a character to an array, so the array is converted to a
                              >string ('Array'), then it takes the
                              >first character ('A') from that string, and put it at position 1.
                              >Hence resulting in '1A'.
                              >>
                              >If you gave us the real code, I'd bet we'd spot where the actual
                              >overriding on $_SESSION["increment"] takes place.
                              >
                              Damn Rik, sometimes you are scaringly smart.
                              Well, that remains to be seen, but thank you, would be nice if I would get
                              paid that way though :-)
                              That sounds like a very probable scenario you described and deducted
                              from the sparse information given.
                              It helps that I cannot get the difference of array_walk() and array_map in
                              my head, so I'm used to seeing some '1''s or 'A's..

                              Now let's get the OP to post the code he uses in it's entirety to claim my
                              fame :-)
                              --
                              Rik Wasmus


                              Comment

                              Working...