开发者

Array inside Array in PHP - How to get the values?

开发者 https://www.devze.com 2023-02-11 23:06 出处:网络
I have a simple code that generate an array inside other array. I\'m with trouble to echo out the values

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,


The values of the array are really only the string "Array". I assume because of the dot here (string concatenation):

$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.


You could write

foreach ($hrefs as $arr)
{
   print_r($arr);
}

or you look inside like this:

echo $hrefs[0][1]; //Only an Example ;-)


Don't do $hrefs[] .= ... but $hrefs[] = ...


$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'));


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'];
....
....
0

精彩评论

暂无评论...
验证码 换一张
取 消