简体   繁体   English

使用数组时发生PHP错误

[英]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']); 错误是由$ids = array_walk('intval', $_POST['id']);

your call to the function array_walk is incorrect 您对array_walk函数的调用不正确

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

try this instead and assuming that $_POST['id'] is an array 试试这个,并假设$_POST['id']是一个数组

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

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

Also it may be good to check before calling the function to make sure that $_POST['id'] is an array 同样最好在调用函数之前进行检查,以确保$_POST['id']是一个数组

EDIT 编辑

After looking at what you doing a little more the function you need to use is array_map . 在看了一下之后,您需要使用的功能是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 . array_walk返回一个布尔值,而array_map返回一个数组,该数组看起来像您需要返回的数组,因为您正在$ids上使用implode

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为每个数组调用一个函数,并将函数作为参数0。我认为您需要更改:

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

Which line is line 36? 哪条线是36号线? Also, you seem to have an extra close parenthesis in the SQL statement. 另外,您似乎在SQL语句中有一个额外的括号。 Furthermore, is the $_POST variable really an array? 此外,$ _POST变量真的是数组吗?

Maybe you have changed the order of arguments in $ids = array_walk('intval', $_POST['id']); 也许您已经更改了$ids = array_walk('intval', $_POST['id']);中参数的顺序$ids = array_walk('intval', $_POST['id']); ? In my opinion it should be $ids = array_walk($_POST['id'],'intval'); 我认为应该是$ids = array_walk($_POST['id'],'intval'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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