Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From my comments:</p> <p><code>"$arrayname"</code> is a string, not an array. <code>call_user_func_array</code> will pass each element in <code>$arr_results</code> as argument to <code>array_intersect</code>. <code>array_intersect</code> expects arrays as arguments, but each item in <code>$arr_results</code> is a string, not an array. </p> <p>All you have to do is create an array of arrays instead of array names:</p> <pre><code>$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); </code></pre> <blockquote> <p>I turn <code>$arrayname</code> into an array with <code>$$arrayname = array();</code></p> </blockquote> <p>Right, you create a variable, lets say <code>arr_0</code> which will point to array. But there is still a difference between the variable name <code>arr_0</code> and the <strong>string</strong> containing the variable name <code>"arr_0"</code>. 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:</p> <pre><code>$arr = "arr_0"; echo $arr[0]; </code></pre> <p>Based on your logic, it should output the first element of the array, but it does not, because <code>$arr</code> is a string, not an array, although it contains the name of a variable. </p> <p>You'd have to use <code>eval</code>, but you really should not. You could also use variable variables again:</p> <pre><code>array_push($arr_results, $$arrayname); </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload