You are here: PHP Array Challenge! « PHP Programming Language « IT news, forums, messages
PHP Array Challenge!

Posted by Oliver Saunders on 10/14/05 12:44

This is an amazing opportunity to win a tin of sardines if you can
answer this difficult question correctly!

Question:
What kind of loop do you need to turn an array like this:

$notes = array(
'1:1',
'1:2',
'1:3'
array(
'2:1',
'2:2',
array(
'3:1',
'3:2'
);
'2:3'
);
'1:4'
array(
'2:4',
'2:5'
)
);

into a list like this:

<ul>
<li>1:1</li>
<li>1:2</li>
<li>1:3</li>
<ul>
<li>2:1</li>
<li>2:2</li>
<ul>
<li>3:1</li>
<li>3:2</li>
</ul>
<li>2:3</li>
</ul>
<li>1:4</li>
<ul>
<li>2:4</li>
<li>2:5</li>
</ul>
</ul>

Obviously in the real world you would probably never encounter this
problem but i'm interested to see if anyone can solve it.

Rules:
- Code must display every element in all the arrays
- Code must nest the lists as the arrays are nested
- Code must be able to adapt to any level of array or list nesting
- <ul>s and <li>s must be terminated correctly with </ul> and </li>
- Not allowed to reformat var_dump(),print_r() or var_export() you
cheeky bastards
- Submissions must be receieved by November 1st and can be direct to
my email ole_v2@dsl.pipex.com if you wish to prevent others stealing
your ideas.

Suggestion:
I thought you might be able to use a foreach loop starting like this:
$parsearray = 'notes';
foreach ($$parsearray as $key => $value) {};
and then change the $$parsearray to the name of the nested array as you
find them and then "continue;". You would need to use a stack go back to
the previous array and position once that particular nest was done. May
or may not be right.

Winner gets a tin of sardines delievered to their door. Tin will be a
lot like this one
http://www.oceanbeauty.com/images/Commander%20Sardines%20side.jpg only
better cause I will have touched it. Code is judged on many areas such
as how compact, elegant and flexible it is.

Good Luck and... Go On!

Ollie

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация