krishna89
krishna89

Reputation: 846

php - comparing and extracting common array elements

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.

Upvotes: 0

Views: 77

Answers (4)

Manjeet Barnala
Manjeet Barnala

Reputation: 2995

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
)

Upvotes: 0

aslawin
aslawin

Reputation: 1981

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.

Upvotes: 2

JYoThI
JYoThI

Reputation: 12085

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);
?>

Upvotes: 0

JesusTheHun
JesusTheHun

Reputation: 1237

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 :)

Upvotes: 3

Related Questions