Jump to content

problem with error - Array to string conversion


ppgpilot

Recommended Posts

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:

[code=php:0]

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

[/code]


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



[code=php:0]

<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>";
}





[/code]




then run the array through a php mail():



[code=php:0]



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
            );

}
}



[/code]

Then the error "Array to string conversion"

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

Thanks

David
Link to comment
Share on other sites

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:

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

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.