gabrielkolbe Posted July 30, 2007 Share Posted July 30, 2007 I have a big struggle on my hands, I have been struggling with this for days now. This is for a test, I have done everything 100% but struggle now with the last bit. I have to create a function with that outputs a list of urls like below with access keys the input array is a multidimentional array. Obviously I have to add a to the input array another element to the array like 'asseskey'=> 'h'. I am struggeling with it - how do I do this in a multi dimential array and then how do I print it out?? $input = array( array('filename' => 'home.html', 'title' => 'home page'), array('filename' => 'products.html', 'title' => 'products'), array('filename' => 'contact.html', 'title' => 'contact'), array('filename' => 'prices.html', 'title' => 'prices')); function add_accesskeys($input) { $output = <<<OUT <ul> <li><a href="home.html" accesskey="h"><em>h</em>ome page</a></li> <li><a href="products.html" accesskey="p"><em>p</em>roducts</a></li> <li><a href="contact.html" accesskey="c"><em>c</em>ontact</a></li> <li><a href="prices.html" accesskey="r">p<em>r</em>ices</a></li> OUT; return $output; } Quote Link to comment https://forums.phpfreaks.com/topic/62571-help-someone-big-struggle-with-multi-dimentional-arrays/ Share on other sites More sharing options...
Nakor Posted July 30, 2007 Share Posted July 30, 2007 <?php $input = array( array('filename' => 'home.html', 'title' => 'home page'), array('filename' => 'products.html', 'title' => 'products'), array('filename' => 'contact.html', 'title' => 'contact'), array('filename' => 'prices.html', 'title' => 'prices')); //echo add_accesskeys($input); function add_accesskeys($arr) { $output = '<ul>'; foreach($arr as $key => $data) { $output .= '<li><a href="'. $data['filename'] .'" accesskey="'. $data['title']{0} .'">'. $data['title'] .'</a></li>'; } $output .= '</ul>'; return $output; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62571-help-someone-big-struggle-with-multi-dimentional-arrays/#findComment-311486 Share on other sites More sharing options...
btherl Posted July 31, 2007 Share Posted July 31, 2007 Nakor's code looks like it'll work. If you want to add the access keys into the array itself, you can do: foreach ($arr as $key => $value) { $arr[$key]['accesskey'] = $arr[$key]['title']{0}; } The {0} takes the first character of a string. Quote Link to comment https://forums.phpfreaks.com/topic/62571-help-someone-big-struggle-with-multi-dimentional-arrays/#findComment-311494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.