<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:series="http://unfoldingneurons.com/"
> <channel><title>Comments on: Flatten Nested Arrays Using&#160;PHP</title> <atom:link href="http://davidwalsh.name/flatten-nested-arrays-php/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name/flatten-nested-arrays-php</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Tue, 22 May 2012 16:21:46 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>By: moli</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-28436</link> <dc:creator>moli</dc:creator> <pubDate>Thu, 05 Jan 2012 03:20:36 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-28436</guid> <description>about the code on very top. their is an issue on using for loop, if the nested array have a key, it wont show the value. Instead use foreach loop. I have some revised on the code above, I hope it will help.function array_flatten($array, $return)
{
foreach($array as $key =&gt; $val)
{
if(is_array($val))
{
$return = array_flatten($val, $return);
}
else
{
if($val)
{
$return[] = $val;
}
}
}
return $return;
}$test= array(&#039;ddd&#039;,
array(&quot;hello&quot; =&gt; &#039;one&#039;,&#039;two&#039;,array(array(&#039;inner&#039;=&gt;&#039;innerval&#039;, &#039;innerval2&#039;,array(&#039;key_inner&#039;=&gt;&#039;inner_inner&#039;)))),&#039;ccc&#039;,
array(&#039;a&#039;,&#039;b&#039;),
array(&#039;dog&#039;,&#039;cat&#039;)
);
$result = array_flatten($test,array());
print_r($result);Output:
Array
(
[0] =&gt; ddd
[1] =&gt; one
[2] =&gt; two
[3] =&gt; innerval
[4] =&gt; innerval2
[5] =&gt; inner_inner
[6] =&gt; ccc
[7] =&gt; a
[8] =&gt; b
[9] =&gt; dog
[10] =&gt; cat
)</description> <content:encoded><![CDATA[<p>about the code on very top. their is an issue on using for loop, if the nested array have a key, it wont show the value. Instead use foreach loop. I have some revised on the code above, I hope it will help.</p><p>function array_flatten($array, $return)<br
/> {</p><p> foreach($array as $key =&gt; $val)<br
/> {<br
/> if(is_array($val))<br
/> {<br
/> $return = array_flatten($val, $return);<br
/> }<br
/> else<br
/> {<br
/> if($val)<br
/> {<br
/> $return[] = $val;<br
/> }<br
/> }<br
/> }<br
/> return $return;<br
/> }</p><p> $test= array(&#8216;ddd&#8217;,<br
/> array(&#8220;hello&#8221; =&gt; &#8216;one&#8217;,'two&#8217;,array(array(&#8216;inner&#8217;=&gt;&#8217;innerval&#8217;, &#8216;innerval2&#8242;,array(&#8216;key_inner&#8217;=&gt;&#8217;inner_inner&#8217;)))),&#8217;ccc&#8217;,<br
/> array(&#8216;a&#8217;,'b&#8217;),<br
/> array(&#8216;dog&#8217;,'cat&#8217;)<br
/> );<br
/> $result = array_flatten($test,array());<br
/> print_r($result);</p><p>Output:<br
/> Array<br
/> (<br
/> [0] =&gt; ddd<br
/> [1] =&gt; one<br
/> [2] =&gt; two<br
/> [3] =&gt; innerval<br
/> [4] =&gt; innerval2<br
/> [5] =&gt; inner_inner<br
/> [6] =&gt; ccc<br
/> [7] =&gt; a<br
/> [8] =&gt; b<br
/> [9] =&gt; dog<br
/> [10] =&gt; cat<br
/> )</p> ]]></content:encoded> </item> <item><title>By: Phil Freo</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-24417</link> <dc:creator>Phil Freo</dc:creator> <pubDate>Wed, 15 Jun 2011 00:13:11 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-24417</guid> <description>Made a few improvements/simplifications, allowed associative arrays, etc.&lt;code&gt;
function array_flatten(array $array, array $return = array()) {foreach ($array as $k =&gt; $item) {
if (is_array($item))
$return = array_flatten($item, $return);
elseif ($item)
$return[] = $item;
}
return $return;
}
&lt;/code&gt;</description> <content:encoded><![CDATA[<p>Made a few improvements/simplifications, allowed associative arrays, etc.</p><p><code><br
/> function array_flatten(array $array, array $return = array()) {</p><p> foreach ($array as $k =&gt; $item) {<br
/> if (is_array($item))<br
/> $return = array_flatten($item, $return);<br
/> elseif ($item)<br
/> $return[] = $item;<br
/> }</p><p> return $return;<br
/> }<br
/> </code></p> ]]></content:encoded> </item> <item><title>By: Ralph Holzmann</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-22315</link> <dc:creator>Ralph Holzmann</dc:creator> <pubDate>Thu, 13 Jan 2011 21:53:53 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-22315</guid> <description>I know this post is a bit old, but here&#039;s what I came up with - it&#039;s super fast and concise.&lt;code&gt;function array_flatten( $array ) {
$arr = array();
function flatten( $item, $key, $flattened ) {
$flattened[] = $item;
};
array_walk_recursive($array, &#039;flatten&#039;, &amp;$arr);
return $arr;
}&lt;/code&gt;</description> <content:encoded><![CDATA[<p>I know this post is a bit old, but here&#8217;s what I came up with &#8211; it&#8217;s super fast and concise.</p><p><code>function array_flatten( $array ) {<br
/> $arr = array();<br
/> function flatten( $item, $key, $flattened ) {<br
/> $flattened[] = $item;<br
/> };<br
/> array_walk_recursive($array, 'flatten', &amp;$arr);<br
/> return $arr;<br
/> }</code></p> ]]></content:encoded> </item> <item><title>By: DkN</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-19078</link> <dc:creator>DkN</dc:creator> <pubDate>Wed, 28 Jul 2010 05:37:11 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-19078</guid> <description>NOTE: this function will omit zero values. for example:$test[0][0] = &#039;0&#039;;
$test[0][1] = &#039;b&#039;;$test[1][0] = &#039;c&#039;;
$test[1][1] = &#039;d&#039;;array_flatten($test) will output:Array ( [0] =&gt; b [1] =&gt; c [2] =&gt; d )to fix this problem, on line 13 use:if(isset($array[$x]))instead of:if($array[$x])here&#039;s the whole function with the line replaced:function array_flatten($array,$return=array())
{
for($x = 0; $x &lt;= count($array); $x++)
{
if(is_array($array[$x]))
{
$return = array_flatten($array[$x],$return);
}
else
{
if(isset($array[$x]))
{
$return[] = $array[$x];
}
}
}
return $return;
}</description> <content:encoded><![CDATA[<p>NOTE: this function will omit zero values. for example:</p><p>$test[0][0] = &#8217;0&#8242;;<br
/> $test[0][1] = &#8216;b&#8217;;</p><p>$test[1][0] = &#8216;c&#8217;;<br
/> $test[1][1] = &#8216;d&#8217;;</p><p>array_flatten($test) will output:</p><p>Array ( [0] =&gt; b [1] =&gt; c [2] =&gt; d )</p><p>to fix this problem, on line 13 use:</p><p>if(isset($array[$x]))</p><p>instead of:</p><p>if($array[$x])</p><p>here&#8217;s the whole function with the line replaced:</p><p>function array_flatten($array,$return=array())<br
/> {<br
/> for($x = 0; $x &lt;= count($array); $x++)<br
/> {<br
/> if(is_array($array[$x]))<br
/> {<br
/> $return = array_flatten($array[$x],$return);<br
/> }<br
/> else<br
/> {<br
/> if(isset($array[$x]))<br
/> {<br
/> $return[] = $array[$x];<br
/> }<br
/> }<br
/> }<br
/> return $return;<br
/> }</p> ]]></content:encoded> </item> <item><title>By: kavs</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-18807</link> <dc:creator>kavs</dc:creator> <pubDate>Wed, 14 Jul 2010 20:27:51 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-18807</guid> <description>This is what i use, sposed to be faster than most methods:function flattenArray(array $array){
$ret_array = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value)
{
$ret_array[] = $value;
}
return $ret_array;
}</description> <content:encoded><![CDATA[<p>This is what i use, sposed to be faster than most methods:</p><p>function flattenArray(array $array){<br
/> $ret_array = array();<br
/> foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value)<br
/> {<br
/> $ret_array[] = $value;<br
/> }<br
/> return $ret_array;<br
/> }</p> ]]></content:encoded> </item> <item><title>By: Mike</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-16758</link> <dc:creator>Mike</dc:creator> <pubDate>Sat, 03 Apr 2010 14:50:52 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-16758</guid> <description>Great function, thanks David.</description> <content:encoded><![CDATA[<p>Great function, thanks David.</p> ]]></content:encoded> </item> <item><title>By: Ennio Wolsink</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-16745</link> <dc:creator>Ennio Wolsink</dc:creator> <pubDate>Fri, 02 Apr 2010 11:54:26 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-16745</guid> <description>@Manu:  if you replace $return[] = $value; with $return[$key] = $value; you get the preserve the index names of the source array(s). Don&#039;t know if that&#039;s everyone preference, but that was what I was looking for. If one wanted to make this optional, he could add a 3rd optional parameter to this function to indicate wether or not to preserve index names, like so:function array_flatten($array, $return=array(), $preserve_index_names = false) {foreach ($array AS $key =&gt; $value) {
if(is_array($value)) {
$return = array_flatten($value,$return, $preserve_index_names);
}
else {
if($value) {
if($preserve_index_names === false) {
$return[] = $value;
}
else {
$return[$key] = $value;
}
}
}
}return $return;
}And then run it like so to get a flattened array back with index names preserved:$result = array_flatten($input, array(), true);And like this if you don&#039;t want the index names preserved:$result = array_flatten($input);</description> <content:encoded><![CDATA[<p>@Manu:  if you replace $return[] = $value; with $return[$key] = $value; you get the preserve the index names of the source array(s). Don&#8217;t know if that&#8217;s everyone preference, but that was what I was looking for. If one wanted to make this optional, he could add a 3rd optional parameter to this function to indicate wether or not to preserve index names, like so:</p><p>function array_flatten($array, $return=array(), $preserve_index_names = false) {</p><p> foreach ($array AS $key =&gt; $value) {<br
/> if(is_array($value)) {<br
/> $return = array_flatten($value,$return, $preserve_index_names);<br
/> }<br
/> else {<br
/> if($value) {<br
/> if($preserve_index_names === false) {<br
/> $return[] = $value;<br
/> }<br
/> else {<br
/> $return[$key] = $value;<br
/> }<br
/> }<br
/> }<br
/> }</p><p> return $return;<br
/> }</p><p>And then run it like so to get a flattened array back with index names preserved:</p><p>$result = array_flatten($input, array(), true);</p><p>And like this if you don&#8217;t want the index names preserved:</p><p>$result = array_flatten($input);</p> ]]></content:encoded> </item> <item><title>By: Chris the Developer</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-13303</link> <dc:creator>Chris the Developer</dc:creator> <pubDate>Thu, 05 Nov 2009 13:27:39 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-13303</guid> <description>David! You just saved me hours!</description> <content:encoded><![CDATA[<p>David! You just saved me hours!</p> ]]></content:encoded> </item> <item><title>By: Manu</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-11810</link> <dc:creator>Manu</dc:creator> <pubDate>Fri, 11 Sep 2009 01:33:25 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-11810</guid> <description>Thanks for the post, this function is very handy.
Although, as we are retrieving a new array with new indexes it might be nice to have the same function that can flatten an associative array.
Here is your function modified to work with associative array:function array&#095;flatten($array, $return=array()) {
foreach ($array AS $key =&gt; $value) {
if(is&#095;array($value))
{
$return = array&#095;flatten($value,$return);
}
else
{
if($value)
{
$return[] = $value;
}
}
}
return $return;}So $myarray = array(&#039;a&#039;,&#039;b&#039;,array(array(&#039;test&#039; =&gt; array(&#039;x&#039;),&#039;y&#039;,&#039;nested2&#039; =&gt; &#039;z&#039;)),array(&#039;nested3&#039; =&gt; array(&#039;p&#039;))) will also work as expected.</description> <content:encoded><![CDATA[<p>Thanks for the post, this function is very handy.<br
/> Although, as we are retrieving a new array with new indexes it might be nice to have the same function that can flatten an associative array.<br
/> Here is your function modified to work with associative array:</p><p>function array&#95;flatten($array, $return=array()) {<br
/> foreach ($array AS $key =&gt; $value) {<br
/> if(is&#95;array($value))<br
/> {<br
/> $return = array&#95;flatten($value,$return);<br
/> }<br
/> else<br
/> {<br
/> if($value)<br
/> {<br
/> $return[] = $value;<br
/> }<br
/> }<br
/> }<br
/> return $return;</p><p>}</p><p>So $myarray = array(&#8216;a&#8217;,'b&#8217;,array(array(&#8216;test&#8217; =&gt; array(&#8216;x&#8217;),&#8217;y',&#8217;nested2&#8242; =&gt; &#8216;z&#8217;)),array(&#8216;nested3&#8242; =&gt; array(&#8216;p&#8217;))) will also work as expected.</p> ]]></content:encoded> </item> <item><title>By: php trivandrum</title><link>http://davidwalsh.name/flatten-nested-arrays-php#comment-10730</link> <dc:creator>php trivandrum</dc:creator> <pubDate>Tue, 28 Jul 2009 02:39:48 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=357#comment-10730</guid> <description>I did a different approach, since the requirement was also different, and the outcome is boasted
about at php-trivandrum, as &lt;a href=&quot;http://www.php-trivandrum.org/tips/function-array&#095;flatten.html&quot; rel=&quot;nofollow&quot;&gt;function array&#095;flatten&lt;/a&gt;</description> <content:encoded><![CDATA[<p>I did a different approach, since the requirement was also different, and the outcome is boasted<br
/> about at php-trivandrum, as <a
href="http://www.php-trivandrum.org/tips/function-array&#95;flatten.html" rel="nofollow">function array&#95;flatten</a></p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 3/7 queries in 0.008 seconds using disk: basic
Object Caching 805/805 objects using disk: basic

Served from: davidwalsh.name @ 2012-05-22 18:10:43 -->
