array-reverse

PHP Need to recursively reverse an array

[亡魂溺海] 提交于 2019-12-31 03:29:06
问题 I need to recursively reverse a HUGE array that has many levels of sub arrays, and I need to preserve all of the keys (which some are int keys, and some are string keys), can someone please help me? Perhaps an example using array_reverse somehow? Also, is using array_reverse the only/best method of doing this? Thanks :) 回答1: Try this: function array_reverse_recursive($arr) { foreach ($arr as $key => $val) { if (is_array($val)) $arr[$key] = array_reverse_recursive($val); } return array_reverse

Reversing an Array in Place why does it not work?

一笑奈何 提交于 2019-12-13 02:27:32
问题 function reverseArray(array) { var reversed_array = []; for(var i = 0; i < array.length; i++) { reversed_array.unshift(array[i]); } return reversed_array; } function reverseArrayInPlace(array) { console.log(array); //[1,2,3,4,5] array = reverseArray(array); console.log(arguments); //{0: [5, 4, 3, 2, 1]} this is good. console.log(array); // [5, 4, 3, 2, 1] this is also good. return array; //should return [5, 4, 3, 2, 1] } console.log(reverseArray(["A", "B", "C"])); //["C", "B", "A"] var