$statues
is an array of multiple objects from a twitter api response.
$status->retweeted_status->user->screen_name
is the username of a Twitter account.
$status->retweeted_status->user->profile_image_url_https
is the image from that user.
What I want is:
As I know, is not possible to have repeated keys into asocciative array, So i wrote down this code:
$vector = [];
foreach($statuses as $status ){
if(array_key_exists ( $status->retweeted_status->user->screen_name ,$vector)){
$vector[$status->retweeted_status->user->screen_name]['cont'] = $vector[$status->retweeted_status->user->screen_name]['cont'] + 1;
} else{
$vector[$status->retweeted_status->user->screen_name] = array(
'img' => str_replace('_normal','_400x400',$status->retweeted_status->user->profile_image_url_https),
'cont' => 1
);
}
}
That's okay, but I want to know:
Is there a better way to Store username, image, and also count how many times does this user appears (Optimize my code)?
How can I order the array to show FIRST the user with most repetitions and last the user with less (Descendent) ?
Thank you!!
To count the User I think what you're looking for is here: Count occurrence of duplicate items in array
Basically use array_count_values
. There's other built in array functions that will order your array for you if needed.