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?

1
  • Do you mean SQL query? SQL isn't really meant ta handle arrays. Commented Nov 6, 2018 at 20:25

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

5 Comments

how can i get only values
so i can use values to retrieve from database
@HemalHerath you can access values directly from an array. Your question is bit unclear. Expected output as per your given sample dataset would be helpful
I have ediited my question can you look it up sir
@HemalHerath nominated for reopening

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.