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

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 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错