Few minutes ago just got a tweet from phpfour (Emran Hasan) that his company is looking for PHP developers. I just visited the announcement page of their site and clicked the apply online button just for testing. There I saw an array written in PHP to solve it. It looked interesting to me and just wrote code for it. Here it is:
[sourcecode language=”php”]
<?php
$array = array(
array(141,151,161),
2,
3,
array(101, 202, 303),
"php",
"5"
);
/**
* print array elements recursively
*
* @param array
*/
function recurse ($array)
{
//statements
foreach ($array as $key => $value)
{
# code…
if( is_array( $value ) )
recurse( $value );
else
echo $key . ‘ => ‘ . $value . ‘<br>’;
}
} // End recurse
recurse($array);
/*
output
0 => 141
1 => 151
2 => 161
1 => 2
2 => 3
0 => 101
1 => 202
2 => 303
4 => php
5 => 5
*/
[/sourcecode]
It’s Fun đ
[code]
āĻāĻāĻāĻž āĻĻāĻŋāĻ¯āĻŧāĻž āĻšāĻ¯āĻŧ āĻ¨āĻž ?
$array = array(
array(141,151,161),
2,
3,
array(101, 202, 303),
"php",
"5"
);
print_r($array);
[/code]
Didn’t you see the post title, it says recursively. The requirement was to print it recursively.
āĻāĻžāĻ āĻŦā§āĻāĻŋ āĻ¨āĻžāĻāĨ¤ āĻā§āĻ¯āĻžāĻā§ āĻŦā§āĻāĻžāĻ āĻĻāĻŋāĻ¯āĻŧā§āĻ¨āĨ¤ āĻāĻŽāĻžāĻ° āĻāĻāĻāĻžāĻ° āĻāĻāĻāĻĒā§āĻ āĻāĻ¸ā§,
Array
(
[0] => Array
(
[0] => 141
[1] => 151
[2] => 161
)
[1] => 2
[2] => 3
[3] => Array
(
[0] => 101
[1] => 202
[2] => 303
)
[4] => php
[5] => 5
)