[英]php - How do I print this multidimensional array?
假设我有以下数组:
Array ( [1284487200] => Array ( [title] => first title [link] => http%3A%2F%2Fexample1.com ) [1261271380] => Array ( [title] => second title [link] => http%3A%2F%2Fexample2.com )
我想像这样打印数组:
<a href="http://example1.com">first title - 1284487200</a><br>
<a href="http://example2.com">second title - 1261271380</a><br>
任何想法将不胜感激!
更新:提供的 2 个答案具有相同的错误,因为我的域包含在链接中,例如。 http://www.mydomain.com/http%3A%2F%example1.com
有什么办法可以纠正吗?
foreach ($array as $key => $entry) {
echo "<a href=\"{$entry['link']}\">{$entry['title']} - {$key}</a><br>";
}
请参阅: http : //php.net/manual/en/control-structures.foreach.php
(最)困难的部分是获取第一维数组的键,这是使用:
$key=>$data
在 foreach 中。
$key 表示正在遍历的数组的键。 $data 内容,在您的示例中 $data 包含第二维数组。
foreach( $array AS $key=>$data )
{
echo '<a href="'.urldecode( $data['link'] ).'">'.$data['title'].' - '.$key.'</a><br>';
}
有关 foreach 语句的更多信息: http : //php.net/manual/en/control-structures.foreach.php
使用foreach
循环, $key
将是数字,其他信息将作为数组在$value
中。 我假设您还想使用urldecode()
解码链接:
foreach($arr_name as $key => $value){
$link = urldecode($value['link']);
echo '<a href="' . $link . '">' . $key . ' - ' . $value['title'] . '</a><br>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.