简体   繁体   中英

PHP error while using array

I get this error from the first line of the following code:

    Fatal error: Only variables can be passed by reference in /home/path/file.php on line 36



        if (isset($_POST['id']))
        {
            $ids = array_walk('intval', $_POST['id']);

            $sql = "DELETE FROM table WHERE id IN (' . implode(',', $ids) . ')')";
            //run query here

            $msg->type = "success";
            $msg->text = "Bulk delete has been successful";
        }

Any ideas what it could be?

BTW, the above code is to mass delete items.

Error is caused by $ids = array_walk('intval', $_POST['id']);

your call to the function array_walk is incorrect

bool array_walk ( array &$array , callback $funcname [, mixed $userdata = NULL ] )

try this instead and assuming that $_POST['id'] is an array

$ids = array_walk($_POST['id'], 'intval');

http://php.net/manual/en/function.array-walk.php

Also it may be good to check before calling the function to make sure that $_POST['id'] is an array

EDIT

After looking at what you doing a little more the function you need to use is array_map . array_walk returns a boolean while array_map returns an array which is what it looks like you need returned since you are using implode on $ids .

So you need to have

$ids = array_map('intval', $_POST['id']);

array_walk calls a function for every array, and takes function as parameter 0. I think you need to change:

array_walk('intval', $_POST['id']); to array('intval', $_POST['id']);

Which line is line 36? Also, you seem to have an extra close parenthesis in the SQL statement. Furthermore, is the $_POST variable really an array?

Maybe you have changed the order of arguments in $ids = array_walk('intval', $_POST['id']); ? In my opinion it should be $ids = array_walk($_POST['id'],'intval'); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM