47861

How to check if multidimensional array contains same value?

Question:

I have a multidimensional array. I need to check if any value in this array has contain same value. If, then execute. What is the better way to check this, or the simplest way TIA

$array[] = array(5, 10, 15, 20, 25, 30); $array[] = array(1, 2, 3, 4, 5, 6); $array[] = array(2, 6, 8, 10, 12, 14); Array ( [0] => Array ( [0] => 5 [1] => 10 [2] => 15 [3] => 20 [4] => 25 [5] => 30 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) [2] => Array ( [0] => 2 [1] => 6 [2] => 8 [3] => 10 [4] => 12 [5] => 14 ) )

Answer1:

If I understood your question correctly, you are looking for a way of finding values that appears in more than one of the inner arrays..? Here are two solutions for that, using some built-in PHP array methods.

<strong>Setup</strong>

<ul><li>Flatten $array (initial step for both methods) using <a href="http://php.net/manual/en/function.array-merge.php" rel="nofollow">array_merge</a> on itself</li> </ul>

Code:

$array[] = array(5, 10, 15, 20, 25, 30); $array[] = array(1, 2, 3, 4, 5, 6); $array[] = array(2, 6, 8, 10, 12, 14, 5); // 5, 10, 15, 20, 25, 30, 1, 2, 3, 4, 5, 6, 2, 6, 8, 10, 12, 14, 5 $array = call_user_func_array('array_merge', $array);

<strong>Method A</strong>

<ol><li>Get an array of <a href="http://php.net/manual/en/function.array-unique.php" rel="nofollow">unique values</a> in $array (duplicates removed) </li> <li>Get what was removed (= the duplicates) by <a href="http://php.net/manual/en/function.array-diff-key.php" rel="nofollow">comparing that array to the original $array</a></li> <li>Make sure values <a href="http://php.net/manual/en/function.array-unique.php" rel="nofollow">appear only once</a> in the final array</li> </ol>

Code:

$duplicates = array_unique( array_diff_key( $array, array_unique($array) ) ); // $duplicates = 5, 2, 6, 10

<strong>Method B</strong>

<ol><li>Get a list of <a href="http://php.net/manual/en/function.array-count-values.php" rel="nofollow">how many times each value appears</a> in $array</li> <li><a href="http://php.net/manual/en/function.array-filter.php" rel="nofollow">Filter</a> that list keeping only values that appears more than once (= duplicates)</li> <li><a href="http://php.net/manual/en/function.array-keys.php" rel="nofollow">Get the keys</a> of that list (the actual $array values)</li> </ol>

Code:

$duplicates = array_keys( array_filter( array_count_values($array), function ($count) { return $count > 1; } ) ); // $duplicates = 5, 10, 2, 6

Answer2:

Just loop through the array and subarray filling $isRepeated with values and frequencies of appearance. When $isRepeated[certain_value] exists means this value was found before:

$array[] = array(5, 10, 15, 20, 25, 30); $array[] = array(1, 2, 3, 4, 5, 6); $array[] = array(2, 6, 8, 10, 12, 14); $isRepeated = array(); foreach($array as $subArray) { foreach($subArray as $item) { if (!isset($isRepeated[$item])) { $isRepeated[$item] = 0; } else { $isRepeated[$item]++; echo "\n<br>Item $item is repeated"; } } }

<a href="http://ideone.com/9yObII" rel="nofollow">http://ideone.com/9yObII</a>

Output:

Item 5 is repeated Item 2 is repeated Item 6 is repeated Item 10 is repeated

Recommend

  • Laravel 4 - Is it possible to extend the DB class?
  • MySQLi: prepared statement to return nested arrays
  • PHP: How do I combine multiple associative arrays into a multidimensional array?
  • Spark - missing 1 required position argument (lambda function)
  • Call dynamic Function with unknown number of params
  • How to get next 2 nodes in HTML + HTMLAgilitypack
  • Why GenericRelation fields does not work in Data Migrations(Django)
  • How can I flatten this array format?
  • array_replace_recursive does not replace an array with some empty array
  • merging two arrays and keep duplicate values
  • turn two nested associative arrays into one flat array?
  • JOOQ nested condition
  • three.js WebVR example code works on threejs.org but not on my local server
  • Wrap assemblies in .NET?
  • Using an enum contained in a Cloud Endpoint model on a Android client
  • Retrieve Facebook Account Information in ios 6
  • retrieving data from url in iphone
  • Get Currently Active User in Android
  • jquery validation - waiting for remote check to complete
  • LiveData is abstract android
  • Compare struct to a constant in C
  • Creating PDF from TIFF image using iText
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • ThreadStatic in asynchronous ASP.NET Web API
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • How does this usort cmp function actually work?
  • Hardware Accelerated Image Scaling in windows using C++
  • Refering to the class itself from within a class mehod in Objective C
  • How to do unit test for HttpContext.Current.Server.MapPath
  • Spring security and special characters
  • Azure Cloud Service Web Role web pages do not load
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Trying to switch camera back to front but getting exception
  • Cannot Parse HTML Data Using Android / JSOUP
  • Free memory of cv::Mat loaded using FileStorage API
  • C# - Getting references of reference
  • Angular 2 constructor injection vs direct access
  • Programmatically clearing map cache