note 61179 deleted from function.array-unique by crell

From: Date: Tue, 12 Apr 2022 00:47:40 +0000
Subject: note 61179 deleted from function.array-unique by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218158@lists.php.net to get a copy of this message
Note Submitter: mcmeijer at yahoo dot com ---- This is a recursive arrayUnique function for arrays of any dimension. (tested with 4-dimensional array) The line '$newArray=deleteEmpty($newArray);' is optional and removes empty keys and values <?php function arrayUnique($myArray) { $newArray = Array(); if (is_array($myArray)) { foreach($myArray as $key=>$val) { if (is_array($val)) { $val2 = arrayUnique($val); } else { $val2 = $val; $newArray=array_unique($myArray); $newArray=deleteEmpty($newArray); break; } if (!empty($val2)) { $newArray[$key] = $val2; } } } return ($newArray); } function deleteEmpty($myArray) { $retArray= Array(); foreach($myArray as $key=>$val) { if (($key<>"") && ($val<>"")) { $retArray[$key] = $val; } } return $retArray; } ?>

« previous php.notes (#218158) next »