嗨,我想知道我可以做些什么来将关联数组中的项目“向上或向下”移动一个,例如对于这个数组:
Array ( [list] =>
Array ( [Name1] => 1,
[Name2] => 1,
[Name3] => 1,
[Name4] => 1,
[Name5] => 1, )
)
我知道如何以non-associative array
这种方式做到这一点:
$a = array('a','b','c','d','e');
function down($a,$x) {
if( count($a)-1 > $x ) {
$b = array_slice($a,0,$x,true);
$b[] = $a[$x+1];
$b[] = $a[$x];
$b += array_slice($a,$x+2,count($a),true);
return($b);
} else {
return $a;
}
}
function up($a,$x) {
if( $x > 0 and $x < count($a) ) {
$b = array_slice($a,0,($x-1),true);
$b[] = $a[$x];
$b[] = $a[$x-1];
$b += array_slice($a,($x+1),count($a),true);
return($b);
} else {
return $a;
}
}
//Use
// Move item 4 up
print_r(up($a,4));