ShadowMage Posted January 27, 2011 Share Posted January 27, 2011 Hey guys, I need to be able to change the index of values in an array. The values must remain the same, but the index pointing to those values needs to change. I've come up with the following solution: $arrIndexChange = array("one" => array(1, 2, 3), "two" => array(7, 8, 9), "three" => array(4, 5, 6));$OldIndex = "two";$NewIndex = "four";$arrIndexChange[$NewIndex] = $arrIndexChange[$OldIndex];unset($arrIndexChange[$OldIndex]); ...which works, but it just seems kind of...kludgy. :)Is there a better way to do this? Link to comment Share on other sites More sharing options...
MrAdam Posted January 27, 2011 Share Posted January 27, 2011 Well there's no need to store the keys within variables... $arrIndexChange['four'] = $arrIndexChange['two'];unset($arrIndexChange['two']); Link to comment Share on other sites More sharing options...
justsomeguy Posted January 27, 2011 Share Posted January 27, 2011 You can write a function if you want, but that's generally the way to do it. Link to comment Share on other sites More sharing options...
iwato Posted January 27, 2011 Share Posted January 27, 2011 Is there a better way to do this?That would depend on how often you must change the key and under what circumstances, would it not?In general, you would not want an array with the same key used twice. So, if you know what the new key should be, you should first check to see that the key does not already exist.Roddy Link to comment Share on other sites More sharing options...
justsomeguy Posted January 27, 2011 Share Posted January 27, 2011 In general, you would not want an array with the same key used twice.Arrays can't have duplicate keys, the second one would replace the first one. The only danger is if you use the same key for both old and new, the end result would be that you just delete that element unless you check if they're the same first.function move_element(&$ar, $old, $new){ if ($old != $new && isset($ar[$old])) { $ar[$new] = $ar[$old]; unset($ar[$old]); }} Link to comment Share on other sites More sharing options...
ShadowMage Posted January 27, 2011 Author Share Posted January 27, 2011 You can write a function if you want, but that's generally the way to do it.Ok, thanks. That's what I wanted to know. ...you should first check to see that the key does not already exist.Can't believe I didn't think of that. Very good point. Thanks! Easy enough to add that check to my function. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 27, 2011 Author Share Posted January 27, 2011 Arrays can't have duplicate keys, the second one would replace the first one.That's what I want to guard against. I don't necessarily want to replace anything.The only danger is if you use the same key for both old and new, the end result would be that you just delete that element unless you check if they're the same first.Another good point. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 27, 2011 Share Posted January 27, 2011 That's what I want to guard against. I don't necessarily want to replace anything.You may at least want to detect that though, so you could have the function above return true if it did the replacement, and false otherwise. So at least you'll be able to tell if it did or did not do the replacement. You could also add a parameter to force replacement if it exists, in case you do want to replace. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 27, 2011 Author Share Posted January 27, 2011 You may at least want to detect that though, so you could have the function above return true if it did the replacement, and false otherwise. So at least you'll be able to tell if it did or did not do the replacement. You could also add a parameter to force replacement if it exists, in case you do want to replace.Yep, that's what I was thinking. :)Thanks guys!! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.