Open In App

PHP | Program to move (key,value) pair upwards or downwards

Last Updated : 14 May, 2018
Improve
Improve
Like Article
Like
Save
Share
Report


Given an array with key, value pair, we need to move specific value upwards or downwards using the key.

Examples:

Input : $continents = array(
                      'First' => 'Asia',
                      'Second' => 'Europe',
                      'Third' => 'North America',
                      'Fourth' => 'South America',
                            );
        move_to_up($continents, 'Third');
Output : Array
(
    [Third] => North America
    [First] => Asia
    [Second] => Europe
    [Fourth] => South America
)

Input :$continents = array(
                      'First' => 'Asia',
                      'Second' => 'Europe',
                      'Third' => 'North America',
                      'Fourth' => 'South America',
                            );
       move_to_bottom($continents, 'Second');
Output : Array
(
    [First] => Asia
    [Third] => North America
    [Fourth] => South America
    [Second] => Europe
)

The above problem can be solved using the PHP function described below :
unset() : The function destroys the specified variable.

Approach : We prepare a temporary array with specified key (value to be moved up or at the bottom), then we unset the key finally we append depending upon the purpose of whether of moving the value up or at bottom.

Below is the implementation of approach
Code 1 : Moving the key, value at the top




<?php
         
        $continents = array(
                      'First' => 'Asia',
                      'Second' => 'Europe',
                      'Third' => 'North America',
                      'Fourth' => 'South America',
                            );
         
        move_to_up($continents, 'Third');
        print_r ($continents);
  
function move_to_up(&$continents, $string)
       {
           $var = array($string => $continents[$string]);
           unset($continents[$string]);
           $continents = $var + $continents;
       }
?>


Output:

Array
(
    [Third] => North America
    [First] => Asia
    [Second] => Europe
    [Fourth] => South America
)

Code 2 : Moving the key, value at the bottom




<?php
         
        $continents = array(
                      'First' => 'Asia',
                      'Second' => 'Europe',
                      'Third' => 'North America',
                      'Fourth' => 'South America',
                            );
       move_to_bottom($continents, 'Second');
       print_r ($continents);
  
 function move_to_bottom(&$continents, $string)
       {
           $var = array($string => $continents[$string]);
           unset($continents[$string]);
           $continents = $continents + $var;
       }
         
?>


Output:

Array
(
    [First] => Asia
    [Third] => North America
    [Fourth] => South America
    [Second] => Europe
)


Similar Reads

How to send dynamic key value pair to PHP with jQuery ?
The purpose of this article is to send dynamic key-value pairs to the PHP back-end using jQuery AJAX in an HTML document. Create two input fields i.e one for a key and the second one for value, and a button (to send key-value pair) in an HTML document. Assign a unique id to both the fields and to the button. In the JavaScript file, add an event lis
3 min read
How to Merge Child Object Value to Parent Key Based on Key Name in JavaScript ?
JavaScript allows us to merge values from child objects into their parent object based on a specific key name. This can be useful for organizing data or simplifying complex structures. There are several approaches available in JavaScript to merge child object values to parent key based on the key name which are as follows. Table of Content Using fo
3 min read
How to find record using any key-value pair information of record in your local/custom database using Node.js ?
The custom database signifies the local database in your file system. There are two types of database ‘SQL’ and ‘NoSQL’. In SQL database data are stored as table manner and in Nosql database data are stored independently with some particular way to identify each record independently. We can also create our own database or datastore locally in Nosql
5 min read
How to add key/value pair to a JavaScript object ?
JavaScript object has key-value pair and it can be of variable length. We first need to declare an object and assign the values to that object for that there can be many methods. In this article, we are going to apply different approaches to adding a key/value pair to a JavaScript object. These are the following methods to add a key/value pair to a
3 min read
How to remove a key-value pair from JavaScript object?
JavaScript object is a powerful data structure that combines keys and values. Sometimes, we need to delete a specific key-value from an object. It can be done using the approaches given below. [caption width="800"]How to remove a key-value from JavaScript object ?[/caption]There are several methods that can be used to remove a key from a JavaScript
7 min read
How to Add a key/value Pair to Map in JavaScript ?
This article will demonstrate how we can add a key-value pair in the JavaScript map. JavaScript Map is a collection of key-value pairs in the same sequence they are inserted. These key values can be of primitive type or the JavaScript object. All methods to add key-value pairs to the JavaScript Map: Table of Content Map constructorUsing map set() M
3 min read
How to Move a Key in an Array of Objects using JavaScript?
The JavaScript array of objects is a type of array that contains JavaScript objects as its elements. You can move or add a key to these types of arrays using the below methods in JavaScript: Table of Content Using Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() methodUsing Object Destructuring and Map()This met
3 min read
Php Program to Move all zeroes to end of array
Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1).Example:   Input :
3 min read
How to search by key=&gt;value in a multidimensional array in PHP ?
In PHP, multidimensional array search refers to searching a key=&gt;value in a multilevel nested array. This search can be done either by the iterative or recursive approach. Recursive Approach: Check if the key exists in a multidimensional array and the value of a key is equal to required one then the result stored in an array and also recur throu
3 min read
How to search by multiple key =&gt; value in PHP array ?
In a multidimensional array, if there is no unique pair of key =&gt; value (more than one pair of key =&gt; value) exists then in that case if we search the element by a single key =&gt; value pair then it can return more than one items. Therefore we can implement the search with more than one key =&gt; value pair to get unique items. Approach: For
3 min read
Article Tags :