note 110590 modified in function.array-intersect by danbrown

From: Date: Thu, 06 Dec 2012 14:19:08 +0000
Subject: note 110590 modified in function.array-intersect by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192321@lists.php.net to get a copy of this message
If you're looking for a relatively easy way to strictly intersect keys and values recursively without array key reordering, here's a simple recursive function: <?php function array_intersect_recursive($array1, $array2) { foreach($array1 as $key => $value) { if (!isset($array2[$key])) { unset($array1[$key]); } else { if (is_array($array1[$key])) { $array1[$key] = array_intersect_recursive($array1[$key], $array2[$key]); } elseif ($array2[$key] !== $value) { unset($array1[$key]); } } } return $array1; } ?> --was-- If you're looking for a relatively easy way to strictly intersect keys and values recursively without array key reordering, here's a simple recursive function: function array_intersect_recursive($array1, $array2) { foreach($array1 as $key => $value) { if (!isset($array2[$key])) { unset($array1[$key]); } else { if (is_array($array1[$key])) { $array1[$key] = array_intersect_recursive($array1[$key], $array2[$key]); } elseif ($array2[$key] !== $value) { unset($array1[$key]); } } } return $array1; } http://php.net/manual/en/function.array-intersect.php

« previous php.notes (#192321) next »