A
A
Alexander2016-11-25 16:55:58
PHP
Alexander, 2016-11-25 16:55:58

How to flip an array correctly?

PHP gurus tell me please, there is a basket in php + AJAX, I write goods to the session, then I display them through AJAX in a loop:
foreach ($_SESSION['basket'] as $key => $value) {
Everything works. Now I want to show the array "back to front" I use:

foreach (array_reverse($_SESSION['basket']) as $key => $value) {

The data is displayed in a cycle "back to front", the same is all ok. Now the task is to get the key, if you use just $key, then it will print 1,2,3,4,5 in order. How to "flip" $key from large to small?
I hope it's clear ^_^

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-11-25
@SmoKE_xD

Parameter list
array: The input array.
preserve_keys: If set to TRUE, the keys will be preserved. Non-numeric keys are not affected by this option and are always preserved.
php.net/manual/en/function.array-reverse.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question