Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

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

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
Fatal error: Cannot use object of type stdClass as array in /home/alylores/public_html/gd/clean2/tryinter.php on line 35 –  Julian Paolo Dayag May 4 '12 at 5:33
    
it's not working sir... fatal error occured –  Julian Paolo Dayag May 4 '12 at 5:35
    
@JulianPaoloDayag, try it now –  Adnan May 4 '12 at 5:37
    
done sir... thanks for the idea... –  Julian Paolo Dayag May 4 '12 at 5:39

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;
}
share|improve this answer
    
i got it already sir... $ids = array(); foreach ($obj->data as $group){ $ids[]= $group->id; } –  Julian Paolo Dayag May 4 '12 at 5:38
1  
@JulianPaoloDayag, you have to thank this guy too.. he helped me to correct the $obj->data –  Adnan May 4 '12 at 5:40
    
yeah sir... but i can only have a answer –  Julian Paolo Dayag May 4 '12 at 5:41
1  
No problem people :-) –  Vikas May 4 '12 at 5:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.