How to Check If Two Arrays are Similar in PHP?


Given two variables in PHP, write a simple function to check if they are similar arrays. Two arrays are similar if they are of same sizes and contain the same elements which may be in different orders.

The implementation is simple – which you just need to sort both arrays, convert them (concatenation) to strings (by using implode) and compare if they are the same. It is necessary to compare the array keys and array values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
/**
 * Determine if two associative arrays are similar
 *
 * Both arrays must have the same indexes with identical values
 * without respect to key ordering 
 * 
 * @param array $a
 * @param array $b
 * @return bool
 */
if (!function_exists("arrays_are_similar")) {
  function arrays_are_similar($a, $b) {
    if (!is_array($a) || (!is_array($b))) return false;
    sort($a);
    sort($b);
    $aa1 = implode(',', array_keys($a));
    $bb1 = implode(',', array_keys($b));
    $aa2 = implode(',', array_values($a));
    $bb2 = implode(',', array_values($b));    
    return ($aa1 === $bb1) && ($aa2 === $bb2);
  }
}
<?php
/**
 * Determine if two associative arrays are similar
 *
 * Both arrays must have the same indexes with identical values
 * without respect to key ordering 
 * 
 * @param array $a
 * @param array $b
 * @return bool
 */
if (!function_exists("arrays_are_similar")) {
  function arrays_are_similar($a, $b) {
    if (!is_array($a) || (!is_array($b))) return false;
    sort($a);
    sort($b);
    $aa1 = implode(',', array_keys($a));
    $bb1 = implode(',', array_keys($b));
    $aa2 = implode(',', array_values($a));
    $bb2 = implode(',', array_values($b));    
    return ($aa1 === $bb1) && ($aa2 === $bb2);
  }
}

To verify if the above works or not, we can write some unit tests (via PHPUnit) to cover different cases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
use PHPUnit\Framework\TestCase;
 
require_once('/var/www/functions/arrays_are_similar.php');
 
class ArraySimilarTests extends TestCase { 
 
  public function dataProvider() {
    return [
        [array(1, 2, 3), array(3, 2, 1), true],
        [array(1, 2, 3, 2), array(3, 2, 1), false],
        [array(2, 3, 2), array(3, 2, 1, 2), false],
        [array(1, 2, 3, 2), array(2, 3, 2, 1), true],
        [array(1, "2", 3), array(3, "2", 1), true],
        [array(1, 2, 3, 4), array(3, 2, 1), false],
        [array(1 => 2, 2 => 3), array(1 => 2, 2 => 3), true],        
        [array(1 => 2, 2 => 3), array(2 => 3, 1 => 2), true],
        [array(1 => 2, 2 => "3"), array(2 => 3, 1 => 2), true],
        [array(1 => 2, 2 => 3), array(2 => 4, 1 => 2), false],
        [null, array(2 => 3, 1 => 2), false],
        [null, null, false]
    ];
  }
  
  /**
   * @dataProvider dataProvider
   */    
  public function testArraySimilar($arr1, $arr2, $result) {
    $this->assertEquals($result, arrays_are_similar($arr1, $arr2));
  }        
}
<?php
use PHPUnit\Framework\TestCase;

require_once('/var/www/functions/arrays_are_similar.php');

class ArraySimilarTests extends TestCase { 

  public function dataProvider() {
    return [
        [array(1, 2, 3), array(3, 2, 1), true],
        [array(1, 2, 3, 2), array(3, 2, 1), false],
        [array(2, 3, 2), array(3, 2, 1, 2), false],
        [array(1, 2, 3, 2), array(2, 3, 2, 1), true],
        [array(1, "2", 3), array(3, "2", 1), true],
        [array(1, 2, 3, 4), array(3, 2, 1), false],
        [array(1 => 2, 2 => 3), array(1 => 2, 2 => 3), true],        
        [array(1 => 2, 2 => 3), array(2 => 3, 1 => 2), true],
        [array(1 => 2, 2 => "3"), array(2 => 3, 1 => 2), true],
        [array(1 => 2, 2 => 3), array(2 => 4, 1 => 2), false],
        [null, array(2 => 3, 1 => 2), false],
        [null, null, false]
    ];
  }
  
  /**
   * @dataProvider dataProvider
   */    
  public function testArraySimilar($arr1, $arr2, $result) {
    $this->assertEquals($result, arrays_are_similar($arr1, $arr2));
  }        
}

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
345 words
Last Post: How to Break Integers (StairCases) using Dynamic Programming?
Next Post: How to Implement a Safe Data Get Function in Javascript?

The Permanent URL is: How to Check If Two Arrays are Similar in PHP?

Leave a Reply