Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-17-2013, 01:57 PM   PM User | #1
cloudstryphe
New Coder

 
Join Date: Mar 2012
Posts: 62
Thanks: 7
Thanked 0 Times in 0 Posts
cloudstryphe is an unknown quantity at this point
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.
cloudstryphe is offline   Reply With Quote
Old 01-17-2013, 02:30 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-18-2013, 07:15 AM   PM User | #3
cloudstryphe
New Coder

 
Join Date: Mar 2012
Posts: 62
Thanks: 7
Thanked 0 Times in 0 Posts
cloudstryphe is an unknown quantity at this point
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;

	}
cloudstryphe is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:12 PM.


Advertisement
Log in to turn off these ads.