<?php
/**
* converts a multidimensional array to a flat array
*
* trying to keep the original names of the keys
* if repeated keys are found a hash will be added to the
* keys trying to keep as much as possible of the original
* key context
*
* september 30 2007
*
* PHP version 5
*
* license GPL
*
*/
$array = array ( 0 => array ( 0 => 1, 1 => 2, 2
=> array ( 0 => 3, 1 => 4, 2 =>
array ( 0 => 5, 1 => 6, 2 => array ( 0 => 7, 1
=> 8,),),), 3 => array (
0 => array ( 0 => 9, 1 => 10, 2 => array ( 0
=> 11, 1 => 12,
2 => array ( 0 => 13, 1 => 14, 2 => array (
0 => 15, 1 => 16,),),),),
1 => array ( 0 => 17, 1 => 18,),),), 1 =>
array ( 0 => 19, 1 => 20,),
2 => array ( 0 => array ( 0 => 21, 1 => 22,
2 => array ( 0 => 23, 1 => 24,
2 => array ( 0 => 25, 1 => 26, 2 => array (
0 => 27, 1 => 28,),),),),
1 => array ( 0 => 29, 1 => 30,),),);
/**
* transforms a multidimensional array to a flat array
*
* the parameter is referenced
* so no returning value is needed
* param array $array the multidimensional array to
flat
* return void
*/
function array_flatten(&$array)
{
function has_arrays($array)
{
foreach ($array as $item) {
if (is_array($item)) {
return true;
}
}
return false;
}
function copy_array(&$array, $array_key)
{
$array2 = $array[$array_key];
unset($array[$array_key]);
foreach ($array2 as $subkey => $subvalue) {
if (array_key_exists($subkey, $array)) {
$array[generate_unique_key($subkey)] =
$subvalue;
} else {
$array[$subkey] = $subvalue;
}
}
}
function generate_unique_key($key)
{
if (strlen($key)>8) {
$key = $key[0] . $key[1] . $key[2];
}
$id = $key . '_';
$uid = uniqid();
$len = strlen($uid);
$max = (9 - strlen($key));
for ($c = $len; ; $c --) {
$id .= $uid[$c];
if ($c == ($len - $max)) {
break;
}
}
return $id;
}
function get_array_indexes($array)
{
$ret_array = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$ret_array[] = $key;
}
}
return $ret_array;
}
while(has_arrays($array)) {
foreach (get_array_indexes($array) as $key) {
copy_array($array, $key);
}
}
}
array_flatten($array);
array_multisort($array);
var_export($array);
/**
* OUTPUT
*
* array (
* 0 => 1,
* '1_403767b6' => 2,
* '0_793767b6' => 3,
* '1_8a3767b6' => 4,
* '0_454767b6' => 5,
* '1_564767b6' => 6,
* '0_035767b6' => 7,
* '1_345767b6' => 8,
* '0_e74767b6' => 9,
* '1_f84767b6' => 10,
* '0_855767b6' => 11,
* '1_a65767b6' => 12,
* '0_4e5767b6' => 13,
* '1_6f5767b6' => 14,
* '0_566767b6' => 15,
* '1_876767b6' => 16,
* '0_5b4767b6' => 17,
* '1_6c4767b6' => 18,
* '0_d43767b6' => 19,
* 1 => 20,
* '0_4e3767b6' => 21,
* '1_5f3767b6' => 22,
* '0_ad4767b6' => 23,
* '1_ce4767b6' => 24,
* '0_485767b6' => 25,
* '1_695767b6' => 26,
* '0_116767b6' => 27,
* '1_426767b6' => 28,
* '0_814767b6' => 29,
* '1_924767b6' => 30,
* )
*/
?>
----
Server IP: 219.94.145.73
Probable Submitter: 220.111.134.171
----
Manual Page -- http://www
.php.net/manual/en/ref.array.php
Edit -- https://master
.php.net/note/edit/77454
Del: integrated -- h
ttps://master.php.net/note/delete/77454/integrated
Del: useless -- http
s://master.php.net/note/delete/77454/useless
Del: bad code -- htt
ps://master.php.net/note/delete/77454/bad+code
Del: spam -- https:/
/master.php.net/note/delete/77454/spam
Del: non-english --
https://master.php.net/note/delete/77454/non-english
Del: in docs -- http
s://master.php.net/note/delete/77454/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/77454
Reject -- https://mast
er.php.net/note/reject/77454
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|