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? 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? 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" } */ 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
Archived
This topic is now archived and is closed to further replies.