php怎么返回相反数组数据,PHP 返回一个单元顺序相反的数组

这篇博客探讨了如何在PHP中交换数组中两个元素的位置,以及如何反转数组。作者提供了两个函数`array_swap_forward`和`array_swap_back`来实现元素交换,并讨论了`array_reverse`函数在处理数字键和字符串键时的行为差异。同时,还展示了一个递归函数用于反转嵌套数组。此外,其他评论者分享了关于数组操作和键值对保持一致性的见解。
摘要由CSDN通过智能技术生成

用户评论:

atulmvAThotmailDOTcoDOTuk (2012-08-29 11:48:15)

Here are a couple of routines to swap the order of two elements in an array

{$ndx=array_search($elem,$arr) -1;$b4=array_slice($arr,0,$ndx);$mid=array_reverse(array_slice($arr,$ndx,2));$after=array_slice($arr,$ndx+2);

returnarray_merge($b4,$mid,$after);

}

functionarray_swap_back($arr,$elem)

{$ndx=array_search($elem,$arr);$b4=array_slice($arr,0,$ndx);$mid=array_reverse(array_slice($arr,$ndx,2));$after=array_slice($arr,$ndx+2);

returnarray_merge($b4,$mid,$after);

}$arr=array('a','b','c','d','e','f');print_r(array_swap_forward($arr,'c'));

echo'
';print_r(array_swap_back($arr,'c'));?>

virtual89 at gmail dot com (2012-07-29 10:35:45)

With array_reverse() if the keys are strings, they remain the same, example:

$a= array ("zero"=>"hello","one"=>"world");var_dump(array_reverse($a));?>

Outputs:

array(2) {

["one"]=>

string(5) "world"

["zero"]=>

string(5) "hello"

}

But if the keys are numbers, they will be changed starting from 0, example:

$a= array (10=>"hello",20=>"world");var_dump(array_reverse($a));?>

Outputs:

array(2) {

[0]=>

string(5) "world"

[1]=>

string(5) "hello"

}

alfbennett at gmail dot com (2010-04-06 13:41:43)

Needed to just reverse keys. Don't flog me if there is a better way. This was a simple solution.

returnarray_reverse(array_reverse($ar,true),false);

}?>

rahulavhad at yahoo dot com (2000-12-29 20:31:09)

This code can help in recursive reversing of the array...

$arr1= array(2,1,array(5,2,1,array(9,8,7)),5,0);$arr1=array_reverse($arr1);

functionReverse_Array($array)

{$index=0;

foreach ($arrayas$subarray)

{    if (is_array($subarray))

{$subarray=array_reverse($subarray);$arr=Reverse_Array($subarray);$array[$index] =$arr;

}

else {$array[$index] =$subarray;}$index++;

}

return$array;

}$arr2=Reverse_Array($arr1);?>

david at audiogalaxy dot com (2000-03-04 01:39:02)

As a further clarification: key-value pairs have an order within an array completely separate from whatever the keys happen to be - the order in which you add them. This is the order that functions like each() and next() will move their pointer through the array.

If you add to an array without specifying the key, like $array[] = value; then an internal counter supplies the key value and then the numerical order of your keys will be identical to the the internal order. If you "leave holes" - jumping ahead by specifying a higher number for the key, like $array[1000] = value; the internal counter gets pushed forward appropriately. Other than its effect on this internal counter, specifying a numerical key seems no different than specifying a string.

However, some array functions, like array_merge() and array_reverse() treat keys that are numbers differently from keys that are not.

david at audiogalaxy dot com (2000-03-04 00:40:43)

With associative arrays array_reverse() keeps key => value pairs matched but reverses the order of the array as spaned by functions like each(). With numerical indexes array_reverse not only reverses position (as spaned by each) but also renumbers the keys.

Both cases seem to be what people would generally want: indeed without the renumbering behavior, someone refering to array elements by numerical key wouldn't think array_reverse did anything.

However, people who are trying to keep numerical keys associated with their values - e.g. trying to have holes in their arrays - will be foiled by the renumbering. The most telling results come from applying array_reverse() to arrays with mixed keys (some numbers and some strings). The strings stay attached and the rest of the keys get renumbered around them - most annoying if you are thinking what you've got is an associative array but some of your keys happen to be numbers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值