Jump to content

Help! someone. Big struggle with multi dimentional arrays


gabrielkolbe

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.