Beispiel #1
0
 /**
  * @covers \CodeLapse\Arr::except
  * @dataProvider provider_personInfo
  */
 public function testExcept($person)
 {
     $original = $person;
     // Simple except
     $excepted1 = Arr::except($person, 'location');
     $this->assertFalse(array_key_exists('location', $excepted1), 'Arr::except 1');
     // Deep except
     $excepted2 = Arr::except($person, 'location.country');
     $this->assertTrue(array_key_exists('location', $excepted2), 'Arr::except 2-1');
     $this->assertFalse(array_key_exists('country', $excepted2['location']), 'Arr::except 2-2');
     // Multiple simple & deep except
     $excepted3 = Arr::except($person, ['tags', 'location.country']);
     $this->assertFalse(array_key_exists('tags', $excepted3), 'Arr::except 3-1');
     $this->assertTrue(array_key_exists('location', $excepted3), 'Arr::except 3-2');
     $this->assertFalse(array_key_exists('country', $excepted3['location']), 'Arr::except 3-3');
     // Check non-destructive except
     $this->assertEmpty(Arr::diffRecursive($original, $person), 'ARR::except 4');
 }
Beispiel #2
0
 /**
  * $array1の中で$array2の中に含まれない要素を返します。
  */
 public static function diffRecursive(array $array1, array $array2)
 {
     // Thanks: http://stackoverflow.com/questions/3876435/recursive-array-diff
     $diff = array();
     foreach ($array1 as $key => $value) {
         // key unmatched but a value in array, it's not diff.
         if (array_key_exists($key, $array2) === false) {
             if (in_array($value, $array2, true)) {
                 continue;
             }
             // key unmatch and value not in array, it's diff.
             $diff[$key] = $value;
             continue;
         }
         // match key and value then not diff
         if ($value === $array2[$key]) {
             continue;
         }
         if (is_array($value)) {
             if (is_array($array2[$key])) {
                 $arrayDiff = Arr::diffRecursive($value, $array2[$key]);
                 if (count($arrayDiff) !== 0) {
                     $diff[$key] = $arrayDiff;
                 }
             }
             continue;
         }
         $diff[$key] = $value;
     }
     return $diff;
 }