Jump to content

Recommended Posts

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;

}

<?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;

}
?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.