2

I have city array like this

$city = array(
        array(1, 2, 3, 4),
        array(1, 2, 3, 4, 5),
        array(2, 3, 4, 5),
    );

I want result like this:

$resulted_array=array(2,3,4);

I want that without using recursive function

4
  • 1
    stackoverflow.com/a/4704211/6797531 Commented Sep 14, 2016 at 11:38
  • this way i am not getting desired output Commented Sep 14, 2016 at 11:39
  • array depth is unlimited , right? Commented Sep 14, 2016 at 11:40
  • yes array lenght is unlimited Commented Sep 14, 2016 at 11:42

3 Answers 3

2
$resulted_array = call_user_func_array('array_intersect',$city);

Array
(
    [1] => 2
    [2] => 3
    [3] => 4
)
Sign up to request clarification or add additional context in comments.

Comments

0

For PHP 5.6 and above, you can directly use array_intersect() with the ... token (also called splat operator in other languages):

$city = array(
    array(1, 2, 3, 4),
    array(1, 2, 3, 4, 5),
    array(2, 3, 4, 5),
);

$inter = array_intersect(...$city);

Performance-wise this is much faster than call_user_func_array()

Comments

0

This is working example for me -

<?php

$arr = array(
        array(1, 2, 3, 4),
        array(1, 2, 3, 4, 5),
        array(2, 3, 4, 5),
    );

$intersect = call_user_func_array('array_intersect', $arr);
print_r($intersect);
?>

O/P

Array
(
    [1] => 2
    [2] => 3
    [3] => 4
)

Function was not required. Only above code will work for your requirement.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.