n00b Weird array addressing issue - what's wrong with my code?

newbstah - January 13, 2008 - 11:05

Hi There -

Need some fresh eyes on this. I've got a block and I've loaded the node into it and have some strange results that I'm not sure how to interpret.

Below is a truncated version of the print_r - I've removed bits that weren't germaine to the conversation and replaced them with '.....' What is confusing me are the [0] => and [1] => right before last comment timestamp and field_journal. I'm trying to dig out the nid of field journal.

At first, I tried:
<?php print $node->field_journal[0]->nid; ?>

Doesn't print anything.

Then I tried
<?php print $node[1]->field_journal[0]->nid; ?>
but got the following error:
Fatal error: Cannot use object of type stdClass as array in /home/devadmin/public_html/includes/common.inc(1348) : eval()'d code on line 10

I was pretty much guessing. I haven't a clue what to do next. Here's the output:

stdClass Object
(
    [nid] => 378
    [vid] => 387
.....
    [body] =>  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
    [teaser] => OMG! I'm so sick of cod!
    [log] =>
    [format] => 3
    [uid] => 21

    [tags] =>
    [nodewords] => Array
        (
        )

    [0] =>
    [last_comment_timestamp] => 1199867783
    [last_comment_name] =>
    [comment_count] => 0
.....
    [1] =>
    [field_journal] => Array
        (
            [0] => Array
                (
                    [nid] => 326
                )

        )

)

I assume this is a print_r

gpk - January 13, 2008 - 12:28

I assume this is a print_r of your $node. Without the missing bits I'm guessing rather but it looks like properties [0] and [1] of $node are empty. Therefore:

$node->field_journal['0']['nid'] might do the trick.

BTW you can't do $node[1] because that's array syntax, not object syntax. Could do $node->{1} though - we need the { } to handle this case where properties are named with numbers.

A tip: if you put <pre>tags round the print_r statement then it's easier to see what's going on.

I guess the confusing part is that the $node object is output by print_r as if it were an array, i.e. using [nid] => 378 etc., even though you have to access it via $node->nid not $node['nid'].

gpk
----
www.alexoria.co.uk

Thank you!

newbstah - January 13, 2008 - 13:05

Thanks so much! Your code worked like a charm and the tips are great. My php is pretty basic, but improving thanks to helpful people like you. Hope something terrific happens to you this week!

Thanks...

gpk - January 16, 2008 - 14:46

for your thanks! It's great when people are appreciative :-)

Also you might want to think about getting a book on PHP for info about language syntax etc. http://php.net is great, but sometimes I find it useful to be able to flick through some real paper pages to get some ideas.!

gpk
----
www.alexoria.co.uk

I was able to use your

Jason Ruyle - January 31, 2008 - 22:18

I was able to use your suggestion for an urgent task. Thanks so much.

------------------------------
i do stuff

:-)

gpk - January 31, 2008 - 23:57
 
 

Drupal is a registered trademark of Dries Buytaert.