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.

图片转代码服务由CSDN问答提供 功能建议

对于令人困惑的标题抱歉...

我需要执行一个 针对可变数量的数组的 array_intersect()。 要做到这一点,我似乎需要使用 call_user_func_array()函数,但是,这似乎不起作用并给我错误:

 <  code>警告:array_intersect()[function.array-intersect]:参数#1不是......中的数组  
 
 

但是,如果我“print_r” 数组,以确保我看到它是一个数组:

 数组([0] =&gt; arr_0 [1] =&gt; arr_1)
   
 
 

我的代码(修剪后只显示损坏的部分):

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

在完整代码中,我使用从sql查询中获取的数据填充foreach循环中的数组。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论

相关推荐 更多相似问题