note 74053 added to function.array-values
| From: | warmo_at_o2_dot_pl at osu1 dot php dot net | Date: | Thu, 22 Mar 2007 06:42:24 +0000 |
| Subject: | note 74053 added to function.array-values | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124698@lists.php.net to get a copy of this message | ||
@Yassin Ezbakhe <yassin88 at gmail dot com>
When we have to flatten multidimensional array of strings or numbers this method could be much
faster.
Inconvenience of this method is, that its speed depends on size of strings/numbers, which array
contains - bigger strings, lower efficiency.
Conclusion: Use this method for small amount of data in arrays (less than 500B per element in my
case) which have many dimensions, in other case, use Yassin Ezbakhe method.
<?php
function md_implode($array, $glue = '')
{
if (is_array ($array))
{
$output = '';
foreach ($array as $v)
{
$output .= md_implode($v, $glue);
}
return $output;
}
else
{
return $array.$glue;
}
}
function md_array_flatten($md_array)
{
$flat_array = explode ('#|#',md_implode($md_array,'#|#')); // "#|#"
is a sample delimiter
array_pop($flat_array); // to remove last empty element
return $flat_array;
}
//Usage:
$flat_array = md_array_flatten($some_md_array)
?>
----
Server IP: 195.136.184.34
Probable Submitter: 62.121.124.204
----
Manual Page -- http://www.php.net/manual/en/function.array-values.php
Edit -- https://master.php.net/note/edit/74053
Del: integrated -- https://master.php.net/note/delete/74053/integrated
Del: useless -- https://master.php.net/note/delete/74053/useless
Del: bad code -- https://master.php.net/note/delete/74053/bad+code
Del: spam -- https://master.php.net/note/delete/74053/spam
Del: non-english -- https://master.php.net/note/delete/74053/non-english
Del: in docs -- https://master.php.net/note/delete/74053/in+docs
Del: other reasons-- https://master.php.net/note/delete/74053
Reject -- https://master.php.net/note/reject/74053
Search -- https://master.php.net/manage/user-notes.php