2

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,

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You could write

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

or you look inside like this:

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

1 Comment

That is not the problem here. print_r prints multidimensional arrays correctly: codepad.org/VjpnVz6s
0

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

Comments

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

Comments

0

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

1 Comment

That is not the problem here. print_r prints multidimensional arrays correctly: codepad.org/VjpnVz6s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.