[php] 變更陣列元素的順序
function change($buff,$new,$old){
$start = $old;
$end = $new;
$keys = array_keys($buff);
$tmp = $keys[$old];
if($new==$old){
return $buff;
}elseif($new<$old){
while($start>=$end){
if($start>$end){
$keys[$start] = $keys[($start-1)];
}elseif($start==$end){
$keys[$start] = $tmp;
}
$start--;
}
}elseif($new>$old){
while($start<=$end){
if($start<$end){
$keys[$start] = $keys[($start+1)];
}elseif($start==$end){
$keys[$start] = $tmp;
}
$start++;
} }
foreach($keys as $key =>$value){
$NEW[$value] = $buff[$value];
}
return $NEW;
}
=======
範例:
$Buff['a'] = "A";
$Buff['b'] = "B";
$Buff['c'] = "C";
$Buff['d'] = "D";
$Buff['e'] = "E";
$Buff['f'] = "F";
$Buff['h'] = "H";
$Buff['i'] = "I";
$Buff['j'] = "J";
$Buff['k'] = "K";
$Buff['l'] = "L";
echo "
";
print_r($Buff);
print_r(change($Buff,6,3));
print_r(change($Buff,2,7));
echo "";
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁