PHP-如何打印此多维数组? [英] 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
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
在foreach中.
$ key表示要遍历的数组的键. $ data的内容,在您的示例中,$ data包含第二维数组.
$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>';
}
有关foreach语句的更多信息: http://php.net/manual/zh-CN/control-structures.foreach.php
More info about the foreach statement: http://php.net/manual/en/control-structures.foreach.php
这篇关于PHP-如何打印此多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!