Jump to content

Move array index to end


arpowers

Recommended Posts

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

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"
}
*/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.