arpowers Posted October 16, 2009 Share Posted October 16, 2009 Hey guys, Quick question: I have an array that looks like this: $var = array( 'name' => 'thename', 'title' => 'thetitle', 'media' => 'themedia' ); and I need to move the 'name' index to the end. Like so: $var = array( 'title' => 'thetitle', 'media' => 'themedia', 'name' => 'thename' ); How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/177878-move-array-index-to-end/ Share on other sites More sharing options...
Mark Baker Posted October 16, 2009 Share Posted October 16, 2009 There are ways, not particularly straightforward though: Why do you need to do it? Quote Link to comment https://forums.phpfreaks.com/topic/177878-move-array-index-to-end/#findComment-937958 Share on other sites More sharing options...
salathe Posted October 16, 2009 Share Posted October 16, 2009 It seems a strange thing to need to do! One quick way would be: $var = array( 'name' => 'thename', 'title' => 'thetitle', 'media' => 'themedia' ); // Remove first element (the name) $name = array_shift($var); // Add it on to the end $var['name'] = $name; var_dump($var); /* array(3) { ["title"]=> string( "thetitle" ["media"]=> string( "themedia" ["name"]=> string(7) "thename" } */ Quote Link to comment https://forums.phpfreaks.com/topic/177878-move-array-index-to-end/#findComment-937989 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.