php PHP中的数组内的数组 - 如何获取值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5015351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array inside Array in PHP - How to get the values?
提问by André
I have a simple code that generate an array inside other array. I'm with trouble to echo out the values
我有一个简单的代码,可以在其他数组中生成一个数组。我很难回显这些值
<?php
require('phpQuery/phpQuery.php');
$doc = phpQuery::newDocumentFileHTML('teste.htm');
//echo $doc['center a']->value('href');
//echo $doc['center a']->text();
foreach ($doc['center a'] as $a) {
$hrefs[] .= array(pq($a)->text() => pq($a)->attr('href'));
}
//$hrefs = array_filter($hrefs);
print_r ($hrefs);
?>
What I got is:
我得到的是:
Array ( [0] => Array [1] => Array [2] => Array [3] => Array [4] => Array [5] => Array [6] => Array [7] => Array [8] => Array [9] => Array [10] => Array [11] => Array [12] => Array [13] => Array [14] => Array [15] => Array [16] => Array [17] => Array [18] => Array [19] => Array [20] => Array [21] => Array [22] => Array [23] => Array [24] => Array [25] => Array [26] => Array [27] => Array [28] => Array [29] => Array [30] => Array [31] => Array [32] => Array [33] => Array [34] => Array [35] => Array [36] => Array [37] => Array [38] => Array [39] => Array [40] => Array [41] => Array [42] => Array [43] => Array [44] => Array [45] => Array [46] => Array [47] => Array [48] => Array [49] => Array [50] => Array [51] => Array [52] => Array [53] => Array [54] => Array [55] => Array [56] => Array [57] => Array [58] => Array [59] => Array [60] => Array [61] => Array [62] => Array [63] => Array [64] => Array [65] => Array [66] => Array [67] => Array )
How can I see the values inside "Array"? Give me some clues.
如何查看“数组”中的值?给我一些线索。
Best Regards,
此致,
回答by Felix Kling
The values of the array are really only the string "Array"
. I assume because of the dot here (string concatenation):
数组的值实际上只是字符串"Array"
。我假设是因为这里的点(字符串连接):
$hrefs[] .= array(pq($a)->text() => pq($a)->attr('href'));
// ^
remove it and it should work. Why did you put it there in the first place?
删除它,它应该可以工作。你一开始为什么把它放在那里?
And it seems unnecessary to create an array with only one element. A better structure might be a one-dimensional array:
而且似乎没有必要创建一个只有一个元素的数组。更好的结构可能是一维数组:
$hrefs[pq($a)->text()] = pq($a)->attr('href');
but that depends on your actual data.
但这取决于您的实际数据。
回答by Tokk
You could write
你可以写
foreach ($hrefs as $arr)
{
print_r($arr);
}
or you look inside like this:
或者你看起来像这样:
echo $hrefs[0][1]; //Only an Example ;-)
回答by mrjames
Don't do $hrefs[] .= ...
but $hrefs[] = ...
不要做$hrefs[] .= ...
但是$hrefs[] = ...
回答by diEcho
use below way to see only
使用下面的方式只看到
for ($i=0,$i<count($hrefs),$i++)
print_r($hrefs[$i])
edit
编辑
then get the value like this
然后得到这样的值
echo $hrefs[0]['foo'];
echo $hrefs[1]['bar'];
....
....
回答by petraszd
$hrefs[] .= array(pq($a)->text() => pq($a)->attr('href'));
.=
is string concat operator. So I assume:
.=
是字符串连接运算符。所以我假设:
$hrefs[] = array(pq($a)->text() => pq($a)->attr('href'));