CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   PHP looping with step of 100 (http://www.codingforums.com/showthread.php?t=285941)

cloudstryphe 01-17-2013 01:57 PM

PHP looping with step of 100
 
Hello everyone. I'm trying to get all the information from the people my user follows on twitter. The problem is with twitter you have to make 2 separate calls to get all the information:

Call 1 returns all the ID's of the users my user follows.

Call 2 returns the user information of up to 100 users which can be asked via a comma separated argument.

So basically I have to get all the user ids from first call and then run 100 at a time through call 2 and put them in another array for storage.

I've been digging at this for a while and haven't been able to come up with a solution. I figured array_splicing might be the way to go but I haven't been able to increment it correctly. This is what I've been able to do so far...

Code:

                $users_array = array();

                $i = 0;

                $tw_user_array = $twitteroauth->get('friends/ids');

                        if($i == 0) {
                                $begin = 0;
                                $end = 100;
                        }

                        else {

                                $begin = $i * 100;
                                $end = $i * 100 + 100;

                        };
                        $user_array = array_splice($tw_user_array->ids, $begin, $end);

                        while (count($user_array) < 101) {

                                array_push($users_array, $user_array);

                                $i++;

                        };


                $cs = implode(",", $users_array);

                $parameters = array('user_id' => "$cs");

                $user_objects = $twitteroauth->get('users/lookup', $parameters);

                return $user_objects;

All help is greatly appreciated. :)

Fou-Lu 01-17-2013 02:30 PM

Is this object member actually an array: $tw_user_array->ids?
Array_splice is probably not what you want. That has the purpose of extracting a part of an array, and then inserting a chunk into it. Doesn't mean it won't work, but it looks to me that you probably want to use array_slice instead which is to remove a part of an array.
PHP Code:

// assuming $tw_user_array->ids member is an array:
$users_array array_slice($tw_users_array->ids$begin$end); 

Simple as that.
This block is unnecessary:
PHP Code:

            if($i == 0) {
                
$begin 0;
                
$end 100;
            }

            else {

                
$begin $i 100;
                
$end $i 100 100;

            }; 

That entire thing can be replaced with:
PHP Code:

$begin $i 100;
$end $i 100 100

If $i is 0 as the first if dictates it could be, than begin and end would be 0 and 100 anyway.

Don't know anything about these objects you are using, but they may have a built in pagination functionality as well.

Also, if you are looping and breaking into chunks of 100 (you don't have anything here that shows this, but it is described as such), simply use array_chunk.

PHP Code:

$tw_user_array $twitteroauth->get('friends/ids');
$aSplitItems array_chunk($tw_user_array->ids100); 

That would give a multidimensional array of 100 items per outer offset. Each inner offset would represent 100 items within it. If there is no way to filter down the results of the get method and work with how you have it designed, than using array_chunk would IMO be the simplest.

cloudstryphe 01-18-2013 07:15 AM

Wow. That was super helpful. I wasn't familiar with the array_chunk function. Really helpful. This is the code that ended up working for me.

Code:

$users_array = array();

$i = 0;

$tw_user_array = $twitteroauth->get('friends/ids');

$chunks = array_chunk($tw_user_array->ids, 100);

foreach($chunks as $chunk) {
                       
        $cs = implode(",", $chunk);       
               
        $parameters = array('user_id' => "$cs");
               
        $user_objects = $twitteroauth->get('users/lookup', $parameters);
                       
        foreach($user_objects as $tw_object) {
                                       
                $tw_array = array ("id" => $tw_object->id,
                                                "name" => $tw_object->name,
                                                "screen_name" => $tw_object->screen_name
                                                );
                                                               
array_push($users_array, $tw_array);
                        }
                       
                }
               
                return $users_array;

        }



All times are GMT +1. The time now is 08:56 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.