From: Florent Bruneau Date: Wed, 28 Apr 2010 19:40:53 +0000 (+0200) Subject: Add pl_flatten to transform arrays containing other arrays into a flat X-Git-Tag: core/1.1.0~24 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=983284ba502852d0d50c9073274d6de3001acab6;p=platal.git Add pl_flatten to transform arrays containing other arrays into a flat array containing only non-array items. e.g.: array(array(1), 2) -> array(1, 2) --- diff --git a/include/platal.inc.php b/include/platal.inc.php index d8155f5..38912a2 100644 --- a/include/platal.inc.php +++ b/include/platal.inc.php @@ -182,6 +182,24 @@ function pl_entity_decode($text, $mode = ENT_COMPAT) return html_entity_decode($text, $mode, 'UTF-8'); } +function pl_flatten_aux(array &$dest, array $src) +{ + foreach ($src as $val) { + if (is_array($val)) { + pl_flatten_aux($dest, $val); + } else { + $dest[] = $val; + } + } +} + +function pl_flatten(array $array) +{ + $res = array(); + pl_flatten_aux($res, $array); + return $res; +} + /** * Returns the path of a static content, including, when appropriate, the * version number. This is used to avoid cross-version cache issues, by ensuiring diff --git a/ut/arraytest.php b/ut/arraytest.php new file mode 100644 index 0000000..4ff151d --- /dev/null +++ b/ut/arraytest.php @@ -0,0 +1,47 @@ +assertSame($res, pl_flatten($src)); + } +} + + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +?>