Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7599625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:42:29+00:00 2026-05-30T22:42:29+00:00

Sorry for the confusing title… I need to perform an array_intersect() against a variable

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:42:31+00:00Added an answer on May 30, 2026 at 10:42 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the confusing title :P I'm sure I should know this but I
Sorry for the somewhat confusing title. Not sure really how to title this. My
Sorry about the confusing title. I recently started doing this in my project, and
Sorry if the title seems confusing, but some examples are in order. Let's say
sorry for the confusing title, its really hard for me to explain what i
Sorry for the confusing title, but it basically says it all. Here's the structures
Sorry if the question title is confusing. Let me explain further. I am building
I'm sorry if the title is quite confusing but I am wondering if it's
Sorry for the confusing title. Basically I made a BinaryReader class that reads in
Sorry for the confusing title. Let me explain via code: #include <string> #include <boost\function.hpp>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.