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

4

2 回答 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

于 2012-05-04T05:30:48.730 回答
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;
}
于 2012-05-04T05:36:35.970 回答