Created
May 11, 2011 16:47
-
-
Save dalethedeveloper/966848 to your computer and use it in GitHub Desktop.
Revisions
-
dalethedeveloper revised this gist
May 11, 2011 . 1 changed file with 17 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,6 @@ function down($a,$x) { } else { return $a; } } function up($a,$x) { if( $x > 0 and $x < count($a) ) { $b = array_slice($a,0,($x-1),true); @@ -43,19 +42,7 @@ function up($a,$x) { ) */ // Move item 4 up print_r(up($a,4)); /* Output Array @@ -67,6 +54,7 @@ function up($a,$x) { [4] => d ) */ // Move item 0 down print_r(down($a,0)); /* Output @@ -79,6 +67,21 @@ function up($a,$x) { [4] => e ) */ // Test, move the bottom item down (ie. do nothing) print_r(down($a,4)); /* Output Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) */ // Test, move the top item up (ie. do nothing) print_r(up($a,0)); /* Output Array -
dalethedeveloper created this gist
May 11, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,92 @@ <?php /* A quick set of functions to move items in a non-associative array up or down by one, shifting the items around it appropriately. Original usage was to for a set of UP and DOWN buttons to manipulate the order of an array of items stored in Wordpress option. */ $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; } } // Up 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; } } // Start print_r($a); /* Output Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) */ // Move item 4 down print_r(down($a,4)); /* Output Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) */ // Move item 5 up print_r(up($a,4)); /* Output Array ( [0] => a [1] => b [2] => c [3] => e [4] => d ) */ // Move item 0 down print_r(down($a,0)); /* Output Array ( [0] => b [1] => a [2] => c [3] => d [4] => e ) */ print_r(up($a,0)); /* Output Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) */