1

when i print_r my array gives

Array ( 
   [0] => Array ( [userid] => 2 [popularity] => 41.7 ) 
   [1] => Array ( [userid] => 5 [popularity] => 33.3 ) 
   [2] => Array ( [userid] => 7 [popularity] => 25.0 ) 
)

is the array returned after querying users and sort by popularity desc, meaning
user with id 2 has popularity 41.7,
user with id 5 has popularity 33.3 and so on,

Then i have a dynamic query that shows each user with his popularity example:
user with id 2 has popularity of 41.7
user with id 5 has popularity of 33.3 etc.

All i want is to show the position(outer array index) of each user if user id of dynamic query matches the output of array above then increment by 1 because array index always starts 0 example:
user with id 2, will have position of 1 (the winner)
user with id 5, will have position of 2
user with id 7, will have position of 3 etc..

how can i do that...

5
  • 1
    what code have you already tried? Commented Apr 18, 2015 at 9:15
  • i have this function: function search_exif($exif, $field) { foreach ($exif as $data) { if ($data['label'] == $field) return $data['raw']['_content']; } }; $camera = search_exif($exif['photo']['exif'], 'model'); then i can search for match, but i am stuck how what can i do Commented Apr 18, 2015 at 9:20
  • 1
    Please do not post additional information as comments here. It is not readable. There is an edit button below your question... Commented Apr 18, 2015 at 9:24
  • Take a look at the various array functions documented on php.net: php.net/manual/de/array.sorting.php Amongst it are functions to sort an array by elements of its content which is what you want. There are many many example alone here on SO and many more millions out on google. Commented Apr 18, 2015 at 9:25
  • Why you can't do $key+1 inside foreach ($array as $key => $value) { ? Commented Apr 18, 2015 at 9:34

1 Answer 1

2

This code modifies array and ads new key 'position' (= $key + 1) to each element.

function addPosition(&$item, $key) {
    $item['position'] = $key + 1;
}
array_walk($data, 'addPosition');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.