Forum Moderators: coopster

Message Too Old, No Replies

Accessing multi-dimensional arrays

Why am I not getting this!

         

Philosopher

12:25 am on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok..I'm playing around with the Google API using PHP and SOAP.

I can run a query and use print_r to get a very ugly output. I'm wanting to do a bit more but am having problems. Here is the situation.

I assign the returned array result to $result. Now, I can do vardump and get everything out of there, but I can't seem to access anything else.

For example, using vardump, it shows one of the keys as "endIndex".

So I try and access $result["endIndex"] but get nothing even though I know it is set.

Even...

If(IsSet($result["endIndex"]))

brings back a false.

All of the search results are in the [resultElements] array inside of $results in the form

$result["resultElements"][0]["URL"]

but I can't access those either.

I've worked with multi-dimensional arrays with no problems so I know I must be doing something stupid that I'm not seeing.

Any thoughts?

ergophobe

1:51 am on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It sure seems like you're doing everything right.

Perhaps you could paste part of your vardump here so we could see exactly what the data looks like.

Tom

Philosopher

2:49 am on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is an exerpt from the vardump with a sample search done. Any help would be greatly appreciated. The vardump shows the results through the first resultelement returned.

It's primarily the info in the ["resultElements"] key I am after, but I just cant seem to figure out where I'm screwin up.


object(stdClass)(11) {
["directoryCategories"]=>
string(1) "
"
["documentFiltering"]=>
bool(false)
["endIndex"]=>
int(10)
["estimateIsExact"]=>
bool(false)
["estimatedTotalResultsCount"]=>
int(43100)
["resultElements"]=>
array(10) {
[0]=>
&object(stdClass)(9) {
["URL"]=>
string(38) "http://www.example.de/blog/"
["cachedSize"]=>
string(3) "90k"
["directoryCategory"]=>
&object(stdClass)(2) {
["fullViewableName"]=>
string(0) ""
["specialEncoding"]=>
string(0) ""
}
["directoryTitle"]=>
string(0) ""
["hostName"]=>
string(0) ""
["relatedInformationPresent"]=>
bool(true)
["snippet"]=>
string(159) "Friday, April 23. 2004. Babylon 5 - Season 4 DVD Box Set. My "Babylon 5 - Season 4 DVD Box Set" just arrived. Posted by sb in movies ... "
["summary"]=>
string(0) ""
["title"]=>
string(29) "my weblog"
}

[edited by: jatar_k at 2:50 am (utc) on April 27, 2004]
[edit reason] generalized urls [/edit]

Philosopher

3:46 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm...noone with any suggestions? Uh oh..

Birdman

4:09 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

You should have said oops, instead of uh oh. ;)

OOP - Object Oriented Programming

If you look at the var dump a little closer, you see that the type of variable is an object, not an array.(object(stdClass)(11) {)

Now I'm no OOP guy, but I'm figuring that access to the data can be acheived like this:

print $result->endIndex;

Here's a read on PHP OOP [liquidpulse.net]. Hope it helps out some.

Added: Just reread you first post and see that you want resultElements, which is an array. Those values would be accessed a little differently, I think.

$result_array = $result->resultElements;
$count = count($result_array);
for ($i=0; $i<$count; $i++) print "<p>".$result_array[$i]."</p>";

Or maybe just:
$count = count($result->resultElements);
for ($i=0; $i<$count; $i++) print "<p>".$result->resultElements[$i]."</p>";

[edited by: Birdman at 4:30 pm (utc) on April 27, 2004]

Philosopher

4:30 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aha....well that would explain the problem.

I tried your suggested code but got an error for a call to an undefined function "resultElements". Oh well.

I'll read through the tutorial you linked to. Hopefully I can get enough of a grasp on OOP from it to do what I need.

Thanks!

Birdman

4:32 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I messed you up. See the edited post. I took the parens off of the expression.

print $result->endIndex;

Hope it works for you.

Philosopher

4:58 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep...I've got it now. Figured that out as well. I've got everything properly now...thanks!