0

I have this foreach loop in PHP where i loop through some array results:

foreach(array_reverse($output) as $row)

but i want to be able to limit the number of results so i can display the results on separate pages

i have tried using the function array_slice in PHP but had no luck, using the below code i just get no results returned

foreach(array_reverse(array_slice($output),0,5) as $row)

how can i limit the results returned in the array?

5
  • You can split an array into chunks with array_chunk Commented Dec 10, 2014 at 20:19
  • 2
    The closing bracket for array_slice is misplaced, array_slice takes 3 arguments. Commented Dec 10, 2014 at 20:19
  • 2
    Where is the information coming from? If it is a database you can limit the amount of rows in your query. Commented Dec 10, 2014 at 20:19
  • If this is from a SQL query, use LIMIT instead Commented Dec 10, 2014 at 20:20
  • Just for example, when running your code I get an error: array_slice() expects at least 2 parameters, 1 given in.... You can see it here: codepad.viper-7.com/g00O6N Commented Dec 10, 2014 at 20:20

1 Answer 1

3

You put the offset and limit in the array_reverse method arguments instead of the array_slice arguments.

array array_reverse ( array $array [, bool $preserve_keys = false ] )

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )

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.