:) A job for ukrainian programmer
Test-task from http://www.dp76.com/php-test/
I wrote this script in three minutes… Maybe I can get a cool php-programmer job
- // http://www.dp76.com/php-test/
- $s='1|0|Electronics
- 2|0|Video
- 3|0|Photo
- 4|1|MP3 player
- 5|1|TV
- 6|4|iPod
- 7|6|Shuffle
- 8|3|SLR
- 9|8|DSLR
- 10|9|Nikon
- 11|9|Canon
- 12|11|20D';
- $s=explode("\n", $s);
- $items=array();
- foreach($s as $item){
- $s1=array();
- $s1=explode('|', trim($item));
- $items[$s1[1]][$s1[0]]=$s1[2];
- }
- echo '<pre>'.CatLst(0).'</pre>';
- function CatLst($parent_id, $indent=''){
- global $items;
- if(isset($items[$parent_id])){
- $out='';
- while(list($item_key, $item_val)=each($items[$parent_id])){
- $out.=$indent.$item_val.' (id: '.$item_key.', parent_id: '.$parent_id.")\r\n";
- $out.=CatLst($item_key, $indent."\t");
- }
- }
- return $out;
- }
Tags: Etc, php, scripts