Array to string conversion error - why?

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

    Array to string conversion error - why?

    I get the following error:

    Notice: Array to string conversion in C:\Documents and
    Settings\ShepMo de\Desktop\Webs ites\ShareMonke y.net\Web\join. php on line 11


    in this code:

    $input = array(array("Fi rst
    Name",$_POST['firstname']),array("Surnam e"=>$_POST['surname']),array("Compan
    y Name"=>$_POST['company_name']),
    array("Website / Application
    URL"=>$_POST['url']),array("Email
    Address"=>$_POS T['email']),array("Userna me"=>$_POST['username']),
    array("Password "=>$_POST['password']));

    (line 11) if (substr_count($ value,"@") != 1) {
    $error_message .= "The email address you have
    entered is invalid.<br>";
    }

    Why does substr_count($v alue,"@") cause this notice? And since it is not an
    error as such, how do I rectify it without modifying php.ini?

    Thanks,

    Keiron


  • Andy Hassall

    #2
    Re: Array to string conversion error - why?

    On Fri, 29 Aug 2003 21:25:50 +0000 (UTC), "Keiron Waites"
    <webmaster@-NOSPAM-sharemonkey.com > wrote:
    [color=blue]
    > I get the following error:
    >
    > Notice: Array to string conversion in C:\Documents and
    >Settings\ShepM ode\Desktop\Web sites\ShareMonk ey.net\Web\join .php on line 11
    >
    >
    >in this code:
    >
    >$input = array(array("Fi rst
    >Name",$_POST['firstname']),array("Surnam e"=>$_POST['surname']),array("Compan
    >y Name"=>$_POST['company_name']),
    > array("Website / Application
    >URL"=>$_POST['url']),array("Email
    >Address"=>$_PO ST['email']),array("Userna me"=>$_POST['username']),
    > array("Password "=>$_POST['password']));
    >
    >(line 11) if (substr_count($ value,"@") != 1) {
    > $error_message .= "The email address you have
    >entered is invalid.<br>";
    > }
    >
    >Why does substr_count($v alue,"@") cause this notice? And since it is not an
    >error as such, how do I rectify it without modifying php.ini?[/color]

    Because $value is an array? Hard to say without seeing where it's assigned...

    Don't hide this warning, since you've probably now counting the number of @
    characters in the string 'Array', which isn't what you want...

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • Keiron Waites

      #3
      Re: Array to string conversion error - why?


      "Andy Hassall" <andy@andyh.co. uk> wrote in message
      news:1givkvktv6 tu6ifunfcfujtdg 4ht4d47vt@4ax.c om...[color=blue]
      > On Fri, 29 Aug 2003 21:25:50 +0000 (UTC), "Keiron Waites"
      > <webmaster@-NOSPAM-sharemonkey.com > wrote:
      >[color=green]
      > > I get the following error:
      > >
      > > Notice: Array to string conversion in C:\Documents and
      > >Settings\ShepM ode\Desktop\Web sites\ShareMonk ey.net\Web\join .php on line[/color][/color]
      11[color=blue][color=green]
      > >
      > >
      > >in this code:
      > >
      > >$input = array(array("Fi rst[/color]
      >
      >Name",$_POST['firstname']),array("Surnam e"=>$_POST['surname']),array("Compa[/color]
      n[color=blue][color=green]
      > >y Name"=>$_POST['company_name']),
      > > array("Website / Application
      > >URL"=>$_POST['url']),array("Email
      > >Address"=>$_PO ST['email']),array("Userna me"=>$_POST['username']),
      > > array("Password "=>$_POST['password']));
      > >
      > >(line 11) if (substr_count($ value,"@") != 1) {
      > > $error_message .= "The email address you have
      > >entered is invalid.<br>";
      > > }
      > >
      > >Why does substr_count($v alue,"@") cause this notice? And since it is not[/color][/color]
      an[color=blue][color=green]
      > >error as such, how do I rectify it without modifying php.ini?[/color]
      >
      > Because $value is an array? Hard to say without seeing where it's[/color]
      assigned...[color=blue]
      >
      > Don't hide this warning, since you've probably now counting the number of[/color]
      @[color=blue]
      > characters in the string 'Array', which isn't what you want...[/color]

      [snip]

      Crap sorry, this: foreach ($input as $name => $value) { assigns the
      $value.

      So:

      $input = array(array("Fi rst
      Name",$_POST['firstname']),array("Surnam e"=>$_POST['surname']),array("Compan
      y Name"=>$_POST['company_name']),
      array("Website / Application
      URL"=>$_POST['url']),array("Email
      Address"=>$_POS T['email']),array("Userna me"=>$_POST['username']),
      array("Password "=>$_POST['password']));
      foreach ($input as $name => $value) {
      (line 11) if (substr_count($ value,"@") != 1) {
      $error_message .= "The email address you have
      entered is invalid.<br>";
      }
      }

      I don't want to hide the error, I want to fix the code :) Is there something
      about the foreach that I don't understand?


      Comment

      • Andy Hassall

        #4
        Re: Array to string conversion error - why?

        On Fri, 29 Aug 2003 21:56:50 +0000 (UTC), "Keiron Waites"
        <webmaster@-NOSPAM-sharemonkey.com > wrote:
        [color=blue]
        >"Andy Hassall" <andy@andyh.co. uk> wrote in message
        >news:1givkvktv 6tu6ifunfcfujtd g4ht4d47vt@4ax. com...[color=green]
        >> On Fri, 29 Aug 2003 21:25:50 +0000 (UTC), "Keiron Waites"
        >> <webmaster@-NOSPAM-sharemonkey.com > wrote:
        >>[color=darkred]
        >> > I get the following error:
        >> >
        >> > Notice: Array to string conversion in C:\Documents and[/color][/color]
        >
        >Crap sorry, this: foreach ($input as $name => $value) { assigns the
        >$value.
        >
        >So:
        >
        >$input = array(array("Fi rst
        >Name",$_POST['firstname']),array("Surnam e"=>$_POST['surname']),array("Compan
        >y Name"=>$_POST['company_name']),
        > array("Website / Application
        >URL"=>$_POST['url']),array("Email
        >Address"=>$_PO ST['email']),array("Userna me"=>$_POST['username']),
        > array("Password "=>$_POST['password']));[/color]

        So $input is an array of arrays...
        [color=blue]
        >foreach ($input as $name => $value) {[/color]

        So each $value is an array.
        [color=blue]
        >(line 11) if (substr_count($ value,"@") != 1) {[/color]

        Running substr_count on an array doesn't make sense; the array gets turned
        into the string 'Array' and a warning output.
        [color=blue]
        >I don't want to hide the error, I want to fix the code :) Is there something
        >about the foreach that I don't understand?[/color]

        Not sure why you've got a load of single-element arrays inside the outer
        array.

        Did you want something more like:

        $input = array(
        "First Name" => $_POST['firstname'],
        "Surname" => $_POST['surname'],
        "Company Name" => $_POST['company_name'],
        "Website / Application URL " => $_POST['url'],
        "Email Address" => $_POST['email'],
        "Username" => $_POST['username'],
        "Password" => $_POST['password'],
        );

        ?

        --
        Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        • Keiron Waites

          #5
          Re: Array to string conversion error - why?


          "Andy Hassall" <andy@andyh.co. uk> wrote in message
          news:6jjvkvgkfd 80t89ik5dmibk58 g6t5kfoti@4ax.c om...[color=blue]
          > On Fri, 29 Aug 2003 21:56:50 +0000 (UTC), "Keiron Waites"
          > <webmaster@-NOSPAM-sharemonkey.com > wrote:
          >[color=green]
          > >"Andy Hassall" <andy@andyh.co. uk> wrote in message
          > >news:1givkvktv 6tu6ifunfcfujtd g4ht4d47vt@4ax. com...[color=darkred]
          > >> On Fri, 29 Aug 2003 21:25:50 +0000 (UTC), "Keiron Waites"
          > >> <webmaster@-NOSPAM-sharemonkey.com > wrote:
          > >>
          > >> > I get the following error:
          > >> >
          > >> > Notice: Array to string conversion in C:\Documents and[/color]
          > >
          > >Crap sorry, this: foreach ($input as $name => $value) { assigns the
          > >$value.
          > >
          > >So:
          > >
          > >$input = array(array("Fi rst[/color]
          >
          >Name",$_POST['firstname']),array("Surnam e"=>$_POST['surname']),array("Compa[/color]
          n[color=blue][color=green]
          > >y Name"=>$_POST['company_name']),
          > > array("Website / Application
          > >URL"=>$_POST['url']),array("Email
          > >Address"=>$_PO ST['email']),array("Userna me"=>$_POST['username']),
          > > array("Password "=>$_POST['password']));[/color]
          >
          > So $input is an array of arrays...
          >[color=green]
          > >foreach ($input as $name => $value) {[/color]
          >
          > So each $value is an array.
          >[color=green]
          > >(line 11) if (substr_count($ value,"@") != 1) {[/color]
          >
          > Running substr_count on an array doesn't make sense; the array gets[/color]
          turned[color=blue]
          > into the string 'Array' and a warning output.
          >[color=green]
          > >I don't want to hide the error, I want to fix the code :) Is there[/color][/color]
          something[color=blue][color=green]
          > >about the foreach that I don't understand?[/color]
          >
          > Not sure why you've got a load of single-element arrays inside the outer
          > array.
          >
          > Did you want something more like:
          >
          > $input = array(
          > "First Name" => $_POST['firstname'],
          > "Surname" => $_POST['surname'],
          > "Company Name" => $_POST['company_name'],
          > "Website / Application URL " => $_POST['url'],
          > "Email Address" => $_POST['email'],
          > "Username" => $_POST['username'],
          > "Password" => $_POST['password'],
          > );
          >
          > ?
          >
          > --
          > Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
          > Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)[/color]

          Aah yes I see the error now (me = retard). Thanks a lot.

          Keiron


          Comment

          Working...