3

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-

2
  • Try: foreach($userFlags['follow'] as $item) { var_dump ($item); } Commented Dec 7, 2013 at 14:26
  • var_dump is similar to print_r - so we just get: code object(stdClass)#116 (7) { ["flagging_id"]=> string(2) "19" ["fid"]=> string(1) "5" ["entity_type"]=> string(4) "user" ["entity_id"]=> string(1) "1" ["uid"]=> string(2) "59" ["sid"]=> string(1) "0" ["timestamp"]=> string(10) "1385845765" } object(stdClass)#117 (7) { ["flagging_id"]=> string(2) "27" ["fid"]=> string(1) "5" ["entity_type"]=> string(4) "user" ["entity_id"]=> string(2) "17" ["uid"]=> string(2) "59" ["sid"]=> string(1) "0" ["timestamp"]=> string(10) "1386167853" } code Commented Dec 7, 2013 at 15:50

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

We throw an error of: Fatal error: Cannot use object of type stdClass as array
0
//@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.
    }

Comments

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.