PHP – Move key value pair to the top/bottom of an array

The following example illustrates how to move a key value pair in an array.

<?php
  $countries = array(
    'CAD' => 'Canada',
    'TWN' => 'Taiwan',
    'USA' => 'United States',
    'GBR' => 'United Kindom',
    'GER' => 'Germany',
  );
  
  print "Original\n";
  print_r($countries);
  
  print "Move Taiwan to the top\n";
  move_to_top($countries, 'TWN');
  print_r($countries);
  
  print "Move United States to the bottom\n";
  move_to_bottom($countries, 'USA');
  print_r($countries);
  
  function move_to_top(&$array, $key) {
    $temp = array($key => $array[$key]);
    unset($array[$key]);
    $array = $temp + $array;
  }
  
  function move_to_bottom(&$array, $key) {
    $value = $array[$key];
    unset($array[$key]);
    $array[$key] = $value;
  }
?>

 

Here is what u got.

 

Done =)

Reference: StackOverflow – Moving array element to top in PHP

6 thoughts on “PHP – Move key value pair to the top/bottom of an array”

  1. Array (
      [0] => Array (
        [Orderid] => 37122
        [Kun.nr] => 1880
        [State] => Otta
        [Land] => America
        [Product_id] => 1092
      )
      [1] => Array (  
        [Orderid] => 37452
        [Kun.nr] => 1680
        [State] => Ottass
        [Land] => Canada
        [Product_id] => 1789
      )
      [2].....
      [3]......... N number of arrays....
    

    Is it possible to move product_id to top….. from Array[0], Array[1], Array[2], Array[N] ? Please help me….

    Thanks in advance.

    Raj

    Like

    1. This should work.

      <?php
        $products = array(
          0 => array(
            'Orderid' => 37122,
            'Kun.nr' => 1880,
            'State' => 'Otta',
            'Land' => 'America',
            'Product_id' => 1092,
          ),
          1 => array(  
            'Orderid' => 37452,
            'Kun.nr' => 1680,
            'State' => 'Ottass',
            'Land' => 'Canada',
            'Product_id' => 1789,
          ),
        );
        
        $new_products = array();
        foreach($products as $product) {
          move_to_top($product, 'Product_id');
          $new_products[] = $product;
        }
      
        print '<pre>';
        print_r($new_products);
        print '</pre>';
        
        function move_to_top(&$array, $key) {
          $temp = array($key => $array[$key]);
          unset($array[$key]);
          $array = $temp + $array;
        }
        
        function move_to_bottom(&$array, $key) {
          $value = $array[$key];
          unset($array[$key]);
          $array[$key] = $value;
        }
      ?>
      
      

      Like

      1. I saved the code in a file called index.php and upload it to the web server and that’s what i get when i browse it in web browser.

        How did you work with it?

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.