hi want know i can move items in associative array 'up or down' 1 example array :
array ( [list] => array ( [name1] => 1, [name2] => 1, [name3] => 1, [name4] => 1, [name5] => 1, ) )
i know how in non-associative array
in way :
$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 , $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 print_r(up($a,4));
try code
$array=array(0=>'name1', 1=>'name2', 2=>'name3', 3=>'name4', 4=>'name5'); function moveelement(&$array, $a, $b) { $out = array_splice($array, $a, 1); array_splice($array, $b, 0, $out); } moveelement($array, 3, 1); print_r($array); result : array ( [0] => name1 [1] => name4 [2] => name2 [3] => name3 [4] => name5 )
Comments
Post a Comment