note 74053 deleted from function.array-values by crell
| From: | crell@php.net | Date: | Tue, 12 Apr 2022 01:04:48 +0000 |
| Subject: | note 74053 deleted from function.array-values by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218185@lists.php.net to get a copy of this message | ||
Note Submitter: warmo_at_o2_dot_pl
----
@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)
?>