0

I have been trying to compare values of inner arrays from a multidimensional array and extract those common values into another array. I have tried using array_intersect along with a foreach loop but this is not giving me result, here the number of inner arrays is dynamic and generated from a different function. Have anyone tried before comparing array elements of a multidimensional array?

My Array:

    $days_filter = array(
    [0] => array(
        '00:00',
        '01:30',
        '02:30',
    ),
    [1] => array(
        '00:00',
        '01:30',
        '03:30',
    ),
    [2] => array(
        '00:30',
        '01:30',
        '02:30',
    ),
    [3] => array(
        '00:30',
        '01:30',
        '04:30',
    ),
);

$res_arr = $days_filter[0];
foreach ($days_filter as $filter) {
    $res_arr = array_intersect($res_arr, $filter);
}

Expected output array:

$res_arr = array(
    [0]=>'01:30'
)

because 01:30 is the common element of all inner arrays.

2
  • 1
    Please provide the expected output Commented May 20, 2016 at 11:31
  • Not a clear question, make it clear with some example. Commented May 20, 2016 at 11:31

4 Answers 4

3

If you are using PHP 5.6+ :

$res_arr = array_intersect(...$days_filter);

Otherwise :

$res_arr = call_user_func_array('array_intersect', $days_filter);

And you are done :)

Sign up to request clarification or add additional context in comments.

Comments

2

You can use this code:

<?php

 $days_filter = array(
    0 => array(
        '00:00',
        '01:30',
        '02:30',
    ),
    1 => array(
        '00:00',
        '01:30',
        '03:30',
    ),
    2 => array(
        '00:30',
        '01:30',
        '02:30',
    ),
    3 => array(
        '00:30',
        '01:30',
        '04:30',
    ),
);


$result = array();

for($j = 0; $j < count($days_filter[0]); $j++)
{
    $tempArray = array();
    for($i = 0; $i < count($days_filter); $i++)
        $tempArray[] = $days_filter[$i][$j];

    if(count(array_unique($tempArray)) == 1)
        $result[] = $tempArray[0];
}

print_r($result);

Working demo: CLICK!!!

It's PHP version independent, so you don't need to think in how PHP5 version this script works.

Comments

0

array_intersect() works for you..

$days_filter = array(
    0 => array(
        '00:00',
        '01:30',
        '02:30',
    ),
    1 => array(
        '00:00',
        '01:30',
        '03:30',
    ),
    2 => array(
        '00:30',
        '01:30',
        '02:30',
    ),
    3 => array(
        '00:30',
        '01:30',
        '04:30',
    ),
);
$first = $days_filter[0];

for($i=1; $i<count($days_filter); $i++)
{
  $result = array_intersect($first, $days_filter[$i]);
  $first = $result;
}

print_r($result);

This will give you :

Array
(
    [1] => 01:30
)

3 Comments

This is not the best answser to be honest... mine is. And yes I'm disappointed that such bad code gets approval.
why did you think that this is bad code ?? @JesusTheHun this code is working perfect try it ...
Semantically meaningless, barely readable and understandable, definitely confusing. A lot of code, some of it absolutely useless, i.e. two vars $first and $result, for something that can be done in one line of code.
0

try something like this source from source

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_intersect($a1,$a2);
print_r($result);
?>

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.