PHP | Program to move (key,value) pair upwards or downwards
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
)
Recommended Posts:
- How to move a file into a different folder on the server using PHP?
- How to add key/value pair to a JavaScript object?
- PHP | Ds\Pair copy() Function
- PHP | Ds\Pair toArray() Function
- PHP | Ds\Pair __construct() Function
- PHP | Ds\Pair clear() Function
- jQuery | Move an element into another element
- Program to get the subdomain of a URL using PHP
- PHP program to check for Anagram
- Program to generate random string in PHP
- PHP Program to count Page Views
- PHP program to change date format
- PHP program to Generate the random number in the given range (min, max)
- PHP program to add item at the beginning of associative array
- PHP program to find missing element(s) from an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



