J
J
John Didact2020-06-30 19:58:54
PHP
John Didact, 2020-06-30 19:58:54

How to solve a simple problem by slicing a part of an array?

There is an array, if it contains more than, say, ten elements, then you need to reduce it to ten, removing the first ones. Decided like this:

if(count($data['ids']) > 10){
 $arr = array_reverse($data['ids']);
 $arr = array_splice($arr, 0, 10);
 $data['ids'] = array_reverse($arr);
}

Is there a simpler solution, in one function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-06-30
@JohnDidact

array_splice($arr, -10)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question