1

I have a array call newArray();

$values = array_count_values($books);

arsort($values);

$newArray = array_keys($values);

it have data like below

Array ( [0] => 37 [1] => 31 [2] => 29 [3] => 28 [4] => 20 [5] => 26 [6] => 34 [7] => 30 [8] => 25 )

how can i get first 5 index in that array and how to write query for those remain values

as example

[0]=>37 from this

get books from books table that id equals to 37 something like that

please help me with this anything will be helpful

EDIT

array have values like this

Array ( [0] => 37 [1] => 31 [2] => 29 [3] => 28 [4] => 20 [5] => 26 [6] => 34 [7] => 30 [8] => 25 )

function like below

if(!empty($books)) {    

            $values = array_count_values($books);

            arsort($values);

            $newArray = array_keys($values);

            $views_books_array = array_slice($newArray, 0, 5);

            $result = $this->db->where_in('book_id',$views_books_array)->get('books');

            print_r($result);
            return $result->result_array();
}

$result not getting what i need what did i do wrong?

HemalHerath
  • 1,006
  • 2
  • 18
  • 38

1 Answers1

1

Try array_slice() function:

// it will get 5 values from $books, starting from 0 offset (first position)
$values = array_count_values(array_slice($books, 0, 5));
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57