PHP | Program to move (key,value) pair upwards or downwards Last Updated : 14 May, 2018 Summarize Comments Improve Suggest changes Share Like Article Like 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 <?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 <?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 ) Comment More infoAdvertise with us Next Article How to check foreach Loop Key Value in PHP ? A akash1295 Follow Improve Article Tags : Web Technologies PHP Similar Reads PHP program to swap two numbers Integer values can be stored in a variable in PHP. It is easy to store and modify and swap these integer values using various mentioned approaches: Using a temporary variable: The value of the first number is stored in the temporary variable. This value is then replaced by the value of the second nu 4 min read How to check foreach Loop Key Value in PHP ? In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such asTable of ContentUsing the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing array_walk Function for IterationUs 3 min read How to use serialize() and unserialize() Function in PHP? In PHP, complex data can not be transported or can not be stored. If you want to execute continuously a complex set of data beyond a single script then these serialize() and unserialize() functions are handy to deal with those complex data structures. The serialize() function just gives a compatible 2 min read PHP program to print an arithmetic progression series using inbuilt functions We have to print an arithmetic progressive series in PHP, between two given numbers a and b both including, a given common arithmetic difference of d. Examples: Input : $a = 200, $b = 250, $d = 10 Output : 200, 210, 220, 230, 240, 250 Input : $a = 10, $b = 100, $d = 20 Output : 10, 30, 50, 70, 90Th 2 min read PHP SplObjectStorage key() Function The SplObjectStorage::key() function is an inbuilt function in PHP which is used to get the index of the currently pointing iterator. Syntax: int SplObjectStorage::key() Parameters: This function does not accept any parameter. Return Value: This function returns the index at which the iterator curre 1 min read Sort an Associative Array by Key in PHP Given an Associative Array, the task is to sort the associative array by its keys in PHP. There are different methods to sort Associative Array by keys, these are described below: Table of ContentUsing ksort() FunctionUsing uksort() FunctionConverting to a Regular Array for SortingUsing array_multis 3 min read Like