Salis Posted September 11, 2007 Share Posted September 11, 2007 I'm having a little trouble writing a script that reorders an array. For an example, let's say I have the following array: Blue Red White Yellow Black Now, what I want to do is move "Yellow" before "Red" I know that "Yellow" will now take the place of "Red" and 1 should be subtracted to the index of each color until the value "Yellow" used to hold. 0 = Blue 1 = Red 2 = White 3 = Yellow 4 = Black I'm guessing any way, the process would be like this: 0 + 0 1 = 3 2 - 1 3 - 1 4 - 0 This should shift the array to look like: Blue Yellow Red White Black or is there an easier way? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/ Share on other sites More sharing options...
cooldude832 Posted September 11, 2007 Share Posted September 11, 2007 are you looking for a random sort/shift or is there a calculated one based on the keys or what? Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346090 Share on other sites More sharing options...
Salis Posted September 11, 2007 Author Share Posted September 11, 2007 No, ordered. I've been working in VB lately, Keys to index... Any way, Yellow is #3 in the array and Red is #1. I want to "move" Yellow before Red. Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346091 Share on other sites More sharing options...
liebs19 Posted September 11, 2007 Share Posted September 11, 2007 I would write a loop, that copies the array into a new array. Then check each time to see if you are the the point to insert yellow. Then insert yellow, then just cop the rest in at its new position 1 spot back. Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346113 Share on other sites More sharing options...
cooldude832 Posted September 11, 2007 Share Posted September 11, 2007 You can sort by a function usign unisort but this isn't exactly a normal function because its not based on y=mx+b where y is the final key m is some slope x is the inital key and B is a constant, here you are moving the keys in a sort of floating pattern. Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346137 Share on other sites More sharing options...
Salis Posted September 11, 2007 Author Share Posted September 11, 2007 People can to this very easily. If we look at 0 - Blue 1 - Red 2 - White 3 - Yellow 4 - Black And want to move Yellow before Black we know that the list will look like 0 - Blue 1 - Yellow 2 - Red 3 - White 4 - Black But to do this in an array, I'd have to list all colors as a key, then their integer as a value. Then add 1 to the array_value up to the Yellows old value.. if that makes any since.... so array('balck'=>0, 'red'=>1, 'white'=>2, 'yellow'=>3, 'black'=>4)... I don't really know, thinking out loud here. Maybe one of you can come up with something Or what if I assign the colors a number and list the values like so. array(0=>0, 1=>2, 2=>3, 3=>2, 4=>4) the keys are the original order and the values are the new number... But I still need a way to add 1... I hope I'm not confusing any one. Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346189 Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 Do you need them to keep the original keys? Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346197 Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 See: http://grady.us/temp/colors.php <?php $colors = array('Blue', 'Red', 'White', 'Yellow', 'Black'); $move = 'Yellow'; $before = 'Red'; //Remove the color you want to move. unset($colors[array_search($move, $colors)]); //Slice out an array from the spot you want to put the color until the end. $endHalf = array_slice($colors, array_search($before, $colors)); //Take out the first half $firstHalf = array_slice($colors, 0, (count($colors)-count($endHalf))); //Assemble the new array $colors = array_merge($firstHalf, array($move), $endHalf); ?> Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346201 Share on other sites More sharing options...
Salis Posted September 11, 2007 Author Share Posted September 11, 2007 Thank you so much. This helps a lot! I started working with a for statement to match keys and values then go through an if else statement..... Your code works much better! Thanks jesirose and everyone. Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346229 Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 No problem. It was a fun challenge. Quote Link to comment https://forums.phpfreaks.com/topic/68857-solved-i-need-help-shiftng-an-array/#findComment-346231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.