From 983284ba502852d0d50c9073274d6de3001acab6 Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Wed, 28 Apr 2010 21:40:53 +0200 Subject: [PATCH] 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) --- include/platal.inc.php | 18 ++++++++++++++++++ ut/arraytest.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 ut/arraytest.php 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: +?> -- 2.1.4