5

i am stuck at this stage of my project.

i am trying to get common values from four multidimensional arrays using array_intersect. can anyone help me with this issue ?

here are all four array:

$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));


$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));


$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));

$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));

$res= array_intersect($arr,$arr1,$arr2,$arr3); 

print_r($res);
5
  • hint: array map serialize array intersect array map unserialize
    – Kevin
    Commented Jul 28, 2014 at 5:17
  • 1
    Is there a reason you are storing every element in its own, single value array? Commented Jul 28, 2014 at 5:17
  • It's wrong approach. Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.. So function would compare 'Array' values instead of real values.
    – sectus
    Commented Jul 28, 2014 at 5:19
  • @MarkM i am not storing any results as shown above... i have shown all arrays as i got in my $wpdb->get_results() in wordpress. Commented Jul 28, 2014 at 5:19
  • @sectus. good point.. but how can i compare those values in my issue ? Commented Jul 28, 2014 at 5:20

2 Answers 2

3

If subarray contain one element always you could chage that value using array_map and current function.

$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));
$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));
$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));

$arr = array_map('current', $arr);   // getting first value of subarray
$arr1 = array_map('current', $arr1);
$arr2 = array_map('current', $arr2);
$arr3 = array_map('current', $arr3);
print_r($arr3);
// Array
// (
//     [0] => 8159
//     [1] => 8140
//     [2] => 8134
//     [3] => 8165
//     [4] => 8166
//     [5] => 8167
//     [6] => 8168
// )

$res= array_intersect($arr,$arr1,$arr2,$arr3);
print_r($res);
// Array
// (
//    [2] => 8134
//    [3] => 8168
// )
0
2

Please check this

$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));
$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));
$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
foreach($arr as $value)
{
    $a1[] = $value[0];
}
foreach($arr1 as $value)
{
    $a2[] = $value[0];
}
foreach($arr2 as $value)
{
    $a3[] = $value[0];
}
foreach($arr3 as $value)
{
    $a4[] = $value[0];
}
$res= array_intersect($a1,$a2,$a3,$a4); 
print_r($res);
0

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.