note 106480 added to function.array-unique

From: Date: Sat, 12 Nov 2011 18:07:29 +0000
Subject: note 106480 added to function.array-unique
Groups: php.notes 
Request: Send a blank email to php-notes+get-184410@lists.php.net to get a copy of this message
Lets say that you want to capture unique values from multidimensional arrays and flatten them in 0 depth. i.e. $tmp = array( 'a' => array( 1,2,3,4 ), 'b' => array( 'c' => array( 4,5,6,7 ) ) ); will return with array_flat( $tmp ) --> array( 1,2,3,4,5,6,7 ); I hope that the function will help someone /** * @params : $a array the recursion array * : $s array storage array * : $l integer the depth level * */ if( !function_exists( 'array_flat' ) ) { function array_flat( $a, $s = array( ), $l = 0 ) { # check if this is an array if( !is_array( $a ) ) return $s; # go through the array values foreach( $a as $k => $v ) { # check if the contained values are arrays if( !is_array( $v ) ) { # store the value $s[ ] = $v; # move to the next node continue; } # increment depth level $l++; # replace the content of stored values $s = array_flat( $v, $s, $l ); # decrement depth level $l--; } # get only unique values if( $l == 0 ) $s = array_values( array_unique( $s ) ); # return stored values return $s; } # end of function array_flat( ... } ---- Server IP: 178.21.169.82 Probable Submitter: 79.107.69.30 ---- Manual Page -- http://www.php.net/manual/en/function.array-unique.php Edit -- https://master.php.net/note/edit/106480 Del: integrated -- https://master.php.net/note/delete/106480/integrated Del: useless -- https://master.php.net/note/delete/106480/useless Del: bad code -- https://master.php.net/note/delete/106480/bad+code Del: spam -- https://master.php.net/note/delete/106480/spam Del: non-english -- https://master.php.net/note/delete/106480/non-english Del: in docs -- https://master.php.net/note/delete/106480/in+docs Del: other reasons-- https://master.php.net/note/delete/106480 Reject -- https://master.php.net/note/reject/106480 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184410) next »