doubingjiu3199 2012-02-26 19:12
浏览 58
已采纳

call_user_func_array + array_intersect有一个数组名称数组,可能吗?

Sorry for the confusing title...

I need to perform an array_intersect() against a variable number of arrays. To do this it seems I need to use the call_user_func_array() function, however, this doesn't seem to be working and gives me the error:

Warning: array_intersect() [function.array-intersect]: Argument #1 is not an array in...

But, if I "print_r" the array to make sure then I see that it is an array:

Array ( [0] => arr_0 [1] => arr_1 ) 

My code (trimmed to just show the broken part):

$i = 0;
$arr_results = array();
foreach($arr_words as $word) {
    $arrayname = "arr_".$i;
    $$arrayname = array();
    while ($row = mysql_fetch_assoc($search)) {
        array_push($$arrayname, $row['id']);
    }
    array_push($arr_results, "$arrayname");
    $i++
}
$matches = call_user_func_array('array_intersect',$arr_results);

In the full code I'm populating the arrays in the foreach loop with data obtained from sql queries.

  • 写回答

1条回答 默认 最新

  • dqkx69935 2012-02-26 19:27
    关注

    From my comments:

    "$arrayname" is a string, not an array. call_user_func_array will pass each element in $arr_results as argument to array_intersect. array_intersect expects arrays as arguments, but each item in $arr_results is a string, not an array.

    All you have to do is create an array of arrays instead of array names:

    $arr_results = array();
    foreach($arr_words as $word) {
        $ids = array();
        while ($row = mysql_fetch_assoc($search)) {
            $ids[] = $row['id'];
        }
        $arr_results[] = $ids;
    }
    $matches = call_user_func_array('array_intersect',$arr_results);
    

    I turn $arrayname into an array with $$arrayname = array();

    Right, you create a variable, lets say arr_0 which will point to array. But there is still a difference between the variable name arr_0 and the string containing the variable name "arr_0". You create an array of strings, and that just won't work. PHP does not know that the string contains a name of a variable. For example, consider this:

    $arr = "arr_0";
    echo $arr[0];
    

    Based on your logic, it should output the first element of the array, but it does not, because $arr is a string, not an array, although it contains the name of a variable.

    You'd have to use eval, but you really should not. You could also use variable variables again:

    array_push($arr_results, $$arrayname);
    

    that would work as well, but as I said, variable variables are confusing and in 99% of the cases, you are better of with an array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀