php - How do I print this multidimensional array?
Lets say I have the following array:
Array ( [1284487200] => Array ( [title] => first title [link] => http%3A%2F%2Fexample1.com ) [1261271380] => Array ( [title] => second title [link] => http%3A%2F%2Fexample2.com )
I want to print the array like this:
<a href="http://example1.com">first title - 1284487200</a><br>
<a href="http://example2.com">s开发者_JAVA技巧econd title - 1261271380</a><br>
Any ideas would be greatly appreciated!
UPDATE: the 2 answers provided have the same error in that my domain is included in the link, eg. http://www.mydomain.com/http%3A%2F%example1.com
Any way I can correct that?
The (most) difficult part is getting the key of the first dimension array, this is get using:
$key=>$data
in the foreach.
$key represents the key of the array being traversed. $data the content, in your example $data contains the second dimension array.
foreach( $array AS $key=>$data )
{
echo '<a href="'.urldecode( $data['link'] ).'">'.$data['title'].' - '.$key.'</a><br>';
}
More info about the foreach statement: http://php.net/manual/en/control-structures.foreach.php
foreach ($array as $key => $entry) {
echo "<a href=\"{$entry['link']}\">{$entry['title']} - {$key}</a><br>";
}
See: http://php.net/manual/en/control-structures.foreach.php
Use a foreach
loop, the $key
will be the number, and the other information will be in $value
as an array. I'm assuming you'll want to decode the link using urldecode()
as well:
foreach($arr_name as $key => $value){
$link = urldecode($value['link']);
echo '<a href="' . $link . '">' . $key . ' - ' . $value['title'] . '</a><br>';
}
精彩评论