Thursday, 12 September 2013

Parsing a Nested Array (PHP)

Parsing a Nested Array (PHP)

I am trying to find an efficient way to do the following :
1) Parse an array. 2) If the element is a single value, store it/echo it.
3) If the element is an array, Parse it and store/echo all of its
elements.
An example would be :
$array = array(15,25,'Dog',[11,'Cat','Cookie15'],22)
This would be echo'd as :
15 25 Dog 11 Cat Cookie15 22
Note : The maximum number of Nested layers of Arrays = 2 (The max is an
Array within an Array, not deeper than that).
The code I have made so far is :
foreach($_POST as $key=>$value){
if(is_array($value))
{
<Not sure how to handle this condition! Need to parse the array and
echo individual elements>
}
else
{
echo "Input name : $key Value : $value ";
}
}

No comments:

Post a Comment