daveferrara1
daveferrara1

Reputation: 97

php print array contents

I need help printing the contents of this function: which came from:

http://drupalcontrib.org/api/drupal/contributions%21flag%21flag.module/function/flag_get_user_flags/7

$userFlags = flag_get_user_flags('user', null, $node->uid, null, false);

If I use print_r:

        print '<pre>';
        print_r(flag_get_user_flags('user', null, $node->uid, null, false));
        print '</pre>';

I get -

Array
(
    [follow] => Array
        (
            [13] => stdClass Object
                (
                    [flagging_id] => 20
                    [fid] => 5
                    [entity_type] => user
                    [entity_id] => 13
                    [uid] => 1
                    [sid] => 0
                    [timestamp] => 1385845849
                )

            [15] => stdClass Object
                (
                    [flagging_id] => 21
                    [fid] => 5
                    [entity_type] => user
                    [entity_id] => 15
                    [uid] => 1
                    [sid] => 0
                    [timestamp] => 1385912237
                )

            [17] => stdClass Object
                (
                    [flagging_id] => 22
                    [fid] => 5
                    [entity_type] => user
                    [entity_id] => 17
                    [uid] => 1
                    [sid] => 0
                    [timestamp] => 1386040495
                )

            [18] => stdClass Object
                (
                    [flagging_id] => 23
                    [fid] => 5
                    [entity_type] => user
                    [entity_id] => 18
                    [uid] => 1
                    [sid] => 0
                    [timestamp] => 1386040515
                )

            [21] => stdClass Object
                (
                    [flagging_id] => 24
                    [fid] => 5
                    [entity_type] => user
                    [entity_id] => 21
                    [uid] => 1
                    [sid] => 0
                    [timestamp] => 1386043939
                )

            [14] => stdClass Object
                (
                    [flagging_id] => 25
                    [fid] => 5
                    [entity_type] => user
                    [entity_id] => 14
                    [uid] => 1
                    [sid] => 0
                    [timestamp] => 1386129658
                )

        )

)

When I use:

foreach($userFlags as $item) {
    echo $item;
}

All i get is the word "Array" printed. If your familiar with drupal ideally I need to convert each entity_id to its author. printing the 13,15 etc. is a good start for me.

thanks for any help-

Upvotes: 3

Views: 5032

Answers (2)

Viren Patel
Viren Patel

Reputation: 43

//@parram $data-array,$d-if true then die by default it is false
 //@author Your name

 function p($data,$d = false){

     echo "<pre>"; 
         print_r($data);
     echo "</pre>"; 

     if($d == TRUE){
        die();
     } 
} // END OF FUNCTION

Use this function every time whenver you need to string or array it will wroks just GREAT. There are 2 Patameters 1.$data - It can be Array or String 2.$d - By Default it is FALSE but if you set to true then it will execute die() function

In your case you can write like this..

 $userFlags = flag_get_user_flags('user', null, $node->uid, null, false);

    foreach($userFlags as $item) {
        p($item['id']); //  If it is array
        p($item->id);   // If it is an Object
                        // To get benefit of this code Use above function of p in your code.
    }

Upvotes: 0

John Conde
John Conde

Reputation: 219804

You have an array in an array. Pull the inner array out before your foreach:

$follow = $userFlags['follow'];
foreach($follow as $item) {
    echo $item->entity_id;
}

Or more succinctly:

foreach($userFlags['follow'] as $item) {
    echo $item->entity_id;
}

Upvotes: 2

Related Questions