note 77671 added to function.array-values

From: Date: Fri, 07 Sep 2007 17:10:11 +0000
Subject: note 77671 added to function.array-values
Groups: php.notes 
Request: Send a blank email to php-notes+get-130297@lists.php.net to get a copy of this message
Most of the array_flatten functions don't allow preservation of keys. Mine allows preserve, don't preserve, and preserve only strings (default). <? // recursively reduces deep arrays to single-dimensional arrays // $preserve_keys: (0=>never, 1=>strings, 2=>always) function array_flatten($array, $preserve_keys = 1, &$newArray = Array()) { foreach ($array as $key => $child) { if (is_array($child)) { $newArray =& array_flatten($child, $preserve_keys, $newArray); } elseif ($preserve_keys + is_string($key) > 1) { $newArray[$key] = $child; } else { $newArray[] = $child; } } return $newArray; } // Tests $array = Array( 'A' => Array( 1 => 'foo', 2 => Array( 'a' => 'bar' ) ), 'B' => 'baz' ); echo 'var_dump($array);'."\n"; var_dump($array); echo 'var_dump(array_flatten($array, 0));'."\n"; var_dump(array_flatten($array, 0)); echo 'var_dump(array_flatten($array, 1));'."\n"; var_dump(array_flatten($array, 1)); echo 'var_dump(array_flatten($array, 2));'."\n"; var_dump(array_flatten($array, 2)); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 63.253.43.10 ---- Manual Page -- http://www.php.net/manual/en/function.array-values.php Edit -- https://master.php.net/note/edit/77671 Del: integrated -- https://master.php.net/note/delete/77671/integrated Del: useless -- https://master.php.net/note/delete/77671/useless Del: bad code -- https://master.php.net/note/delete/77671/bad+code Del: spam -- https://master.php.net/note/delete/77671/spam Del: non-english -- https://master.php.net/note/delete/77671/non-english Del: in docs -- https://master.php.net/note/delete/77671/in+docs Del: other reasons-- https://master.php.net/note/delete/77671 Reject -- https://master.php.net/note/reject/77671 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130297) next »