0

i fetch the user's group in facebook using graph api...
http://developers.facebook.com/tools/explorer?method=GET&path=100000004811603%2Fgroups / with user_groups permission and a valid access_token

i turned it into array using this function i found in google:

 function objectToArray($d) {
        if (is_object($d)) {
        // Gets the properties of the given object
        // with get_object_vars function
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        /*
        * Return array converted to object
        * Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return array_map(__FUNCTION__, $d);
    }
    else {
        // Return array
        return $d;
    }
}

now i have the the results in this variable

$groups = objectToArray($obj);

and now what i want to do is to extract the values of id keys and put it in an array that will looks like this:

$new = array("group id 1","group id 2","group id 3","group id 4"); // and so on....

can someone help me plsss

10
  • Why are you converting from an object to an array? What does the object look like? Can you run var_dump($obj);? Commented May 4, 2012 at 5:16
  • i think you may use json while using facebook graph, Please share what you exactly want to do? Commented May 4, 2012 at 5:20
  • @jonathan i've edited my post sir... there is a link below.. Commented May 4, 2012 at 5:21
  • @rohit i want to gather the group ids so that i can use them to post in each group... Commented May 4, 2012 at 5:22
  • @RohitKumarChoudhary the $obj is the variable with the json_decode() of the results of the graph api Commented May 4, 2012 at 5:25

2 Answers 2

2

Try this:

$ids=array();
foreach ($obj->data as $group){
$ids[]=$group->id;
}

as you can see we're taking the json_decode() result and iterating over all the elements (groups) and storing only the id into the array $ids

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

1 Comment

Fatal error: Cannot use object of type stdClass as array in /home/alylores/public_html/gd/clean2/tryinter.php on line 35
2

You do not need to convert complete $obj to an array. You can access its members with -> operator. So to collect all the id member of data member of $obj, you have to do something like:

$group_ids = array();
foreach ($obj->data as $group_data) {
  $group_ids[] = $group_data->id;
}

3 Comments

i got it already sir... $ids = array(); foreach ($obj->data as $group){ $ids[]= $group->id; }
@JulianPaoloDayag, you have to thank this guy too.. he helped me to correct the $obj->data
yeah sir... but i can only have a answer

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.