Jump to content


Photo

problem with error - Array to string conversion


  • Please log in to reply
1 reply to this topic

#1 ppgpilot

ppgpilot
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 17 October 2006 - 05:57 AM

I have a problem and cannot find the answer...

I am receiving an error - "Array to string conversion"

on the page head I have the following which is throwing the error:


$_POST = array_map('stripslashes', $_POST);



I am using a form multiple select box to select emails into an array:




<select multiple name="client_email[]" size="3" >


EOT;

$sql = ("select client_name, email from $tbl1");

$result = mysql_query($sql);


while ($row = mysql_fetch_array($result))
{
    $client_name = $row['client_name'];
    $email = $row['email'];

    echo "<option value=\"$email\">$client_name</option>";
}









then run the array through a php mail():






if(isset($_POST['client_email']))
{
if(is_array($_POST['client_email']))
{
        $client_email = $_POST['client_email'];


        foreach ($client_email as $email)
        {
            mail(
            $email,
            $subject,
            $client_message,
            $header
            );
         }
}elseif(!is_array($_POST['client_email']))
{

        $email = $_POST['client_email'];

            mail(
            $email,
            $subject,
            $client_message,
            $header
            );

}
}




Then the error "Array to string conversion"

If you have the answer and a fix, please let me know

Thanks

David


#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 17 October 2006 - 07:27 AM

i have a feeling that it's because array_map() only works one level deep.  in other words, it strictly runs stripslashes() on $_POST['client_email'], not each of the $_POST['client_email'][]s themselves.  therefore you're trying to convert the $_POST['client_email'] array into a string when passing it to stripslashes(), which expects a string.

try replacing it with:

$_POST['client_email'] = array_map('stripslashes', $_POST['client_email']);

you can also have a look in the comments in the php manual for array_map() for "deep_map."  someone wrote a recursive function using array_map() so that it maps the given function to all levels of an array.  i imagine this will be a better solution in the long run.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users