Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    63
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    63
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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;
    
    	}


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •