0

I would like to find out a duplicate value in the all nested arrays within an array. At the moment my array is something like that.

Array $bigarray = Array (
    [431] => Array (
        [0] => orange 
        [1] => apple 
        [2] => pine
    ) 
    [440] => Array ( 
        [0] => orange 
        [1] => lilly 
    ) 
    [444] => Array (  
        [0] => orange 
        [1] => pine 
    ) 
)

I would like to extract only orange which is in all

arrays('431','440','444').

Woudl you give me some idea...? Thanks in advance.

0

4 Answers 4

10

You can use array_intersect():

$intersected = null;
foreach ($bigarray as $arr) {
  $intersected = $intersected ? array_intersect($arr, $intersected) : $arr;
  if (!$intersected) {
    break; // no reason to continue
  }
}
print_r($intersected);

Array
(
  [0] => orange
)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks this is exactly what I wanted!! by the way i want to know how to delete the extracted items from all the arrays. i tried foreach ($clauses as $arr){..unset.} but no luck.
2
$inAllChunks = call_user_func_array('array_intersect',(array_values($bigarray)));
var_dump($inAllChunks);

Comments

1
$output = null;

foreach ( $bigarray as $array ) {
  if ( is_null($output) ) {
    $output = $array;
    continue;
  }

  $output = array_intersect($output, $array);
  if ( empty($output) ) {
    break;
    // there are no common elements in the array
  }
}

var_dump$(output);

Comments

1

From the documentation.

$array1 = array("a" => "green", "red", "blue");  
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);

http://www.php.net/manual/en/function.array-intersect.php

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.