0

I have five arrays and a search for which user can do search randomly. So for among those five sometimes there may be value for two arrays, three arrays or five arrays and whatever.

So When I intersect I am not be able to check which are empty so that it always returns an empty array.

$full_ids = array_intersect($g_arr, $c_arr, $k_arr, $m_arr, $p_arr);

Actually I need to check and make this dynamic like if there are values for $g_arr, $c_arrthen the above operation will be applied with these two.. like

$full_ids = array_intersect($g_arr, $c_arr);

I don't understand how to check that? Any help w'd be appreciated..thanks

5
  • Probably you have to use array_diff, not array_intersect Commented Mar 17, 2014 at 8:16
  • @hindmost I need the common values from those arrays... Commented Mar 17, 2014 at 8:18
  • Create a new, empty array; Test each array in turn, if it contains values then push to your new array; then use call_user_func_array() to call array_intersect() with your new array as the argument Commented Mar 17, 2014 at 8:20
  • It's hard to understand what you want. Could you provide example of source arrays and desired result? Commented Mar 17, 2014 at 8:20
  • @MarkBaker w'd you please write a sample here... Commented Mar 17, 2014 at 8:25

1 Answer 1

1
$tempArray = [];
if (count($g_arr) >0) $tempArray[] = $g_arr;
if (count($c_arr) >0) $tempArray[] = $c_arr;
if (count($k_arr) >0) $tempArray[] = $k_arr;
if (count($m_arr) >0) $tempArray[] = $m_arr;
if (count($p_arr) >0) $tempArray[] = $p_arr;

$intersect = call_user_func_array('array_intersect', $tempArray);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.