Reset keys of array elements using PHP ? Last Updated : 12 Feb, 2019 Comments Improve Suggest changes Like Article Like Report In PHP, two types of arrays are possible indexed and associative array. In case of Indexed array, the elements are strictly integer indexed and in case of associative array every element has corresponding keys associated with them and the elements can only be accessed using these keys. To access the keys and values of an associative array using integer index the array_keys() and array_values() inbuilt functions can be used. The array_keys() function takes input an array and returns the indexed array of only keys from the input array. The array_values() function takes input an array and return the indexed array of only values from the input array. Now, to reset the keys of array elements two operations can be performed, key_swap() and key_change(). As these are not inbuilt function these has to implemented in the code. Using key_swap() function: This function will take input an array and two keys and then swap the values corresponding to these two keys with the help of another variable ($val) and returns the resulting array. Note: This function will throw an error if both the keys are not present in the array. Using key_change() function: This function will take input an array and two keys, one old key (already present in the array) and a new key. The function will first store the value corresponding to the old key in to a third variable ($val) and then delete (unset()) the old key and its corresponding value from the array. Then the new key will be added to the array the value stored in the third variable ($val) will be assigned to it and the resulting array will be returned. Note: This function will return desired output if the new key is not present in the array otherwise if the new key in present in the input array then value of the new key will be lost as the value of the old key will overwrite it. Also, the function will throw an error if the old key is not present in the input array. Program: PHP program to reset keys of array element in an array. php <?php // PHP program to reset the keys of array elements // Function to swap the values of any // two keys in an array function key_swap($arr, $key1, $key2) { $val = $arr[$key1]; $arr[$key1] = $arr[$key2]; $arr[$key2] = $val; return $arr; } // Function to change the key corresponding // to the value in an array function key_change($arr, $oldkey, $newkey){ $val = $arr[$oldkey]; unset($arr[$oldkey]); $arr[$newkey] = $val; return $arr; } // Sample associative array $arr = array( 'zero' => 1, 'one' => 2, 'two' => 0, 'test' => 3 ); // Print the sample array echo "The Sample array: "; print_r($arr); // Swap the keys 'zero' and 'one' $arr = key_swap($arr, 'zero', 'one'); // Swap the keys 'zero' and 'two' $arr = key_swap($arr, 'zero', 'two'); // Change the key 'test' to 'three' $arr = key_change($arr, 'test', 'three'); // Print the modified array echo "The Modified array: "; print_r($arr); ?> Output: The Sample array: Array ( [zero] => 1 [one] => 2 [two] => 0 [test] => 3 ) The Modified array: Array ( [zero] => 0 [one] => 1 [two] => 2 [three] => 3 ) Comment More infoAdvertise with us Next Article Reset keys of array elements using PHP ? A Arghyadip_Manna Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads Program to remove empty array elements in PHP Given an array containing elements. The task is to remove empty elements from the array such as an empty string or a NULL element. Method 1: Using array_filter() Function. It is achieved by using array_filter() function. It also removes false values when declared using a callback function, however, 2 min read Remove an Elements From End of an Array in PHP Given an array "myArray", the task is to remove an element from its end. This operation is important in scenarios involving stacks, queues, and web development. In PHP, there are several approaches to achieve this One common method is using the array_pop() function, which removes the last element fr 3 min read Swipe First & Last Element of an Array in PHP Swapping the first and last elements of an array is a common operation that can be useful in various scenarios, such as rearranging data or preparing it for specific algorithms. Below are the approaches to swipe the first and last element of an array in PHP: Table of Content Using Temporary Variable 3 min read How to Print Unique Elements an Given Array in PHP? Given an array, the task is to print all distinct elements of the given array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted.Examples: Input: arr[] = {5, 6, 5, 3, 6, 4, 3, 5}Output: 5, 6, 3, 4Input: arr[] = {1, 2, 3, 4, 5, 5 2 min read Remove Specific Element from an Array in PHP Given an Array containing some elements, the task is to remove specific elements from the array in PHP. Below are the approaches to remove specific elements from an array in PHP: Table of Content Using unset() FunctionUsing array_diff() FunctionUsing array_filter() FunctionUsing array_splice() Funct 3 min read How to Add Elements to the End of an Array in PHP? In PHP, arrays are versatile data structures that can hold multiple values. Often, you may need to add elements to the end of an array, whether it's for building a list, appending data, or other purposes. PHP provides several ways to achieve this. In this article, we will explore different approache 3 min read How to get the First Element of an Array in PHP? Accessing the first element of an array in PHP is a common task, whether they are indexed, associative, or multidimensional. PHP provides various methods to achieve this, including direct indexing, and built-in functions.Below are the approaches to get the first element of an array in PHP:Table of C 4 min read Remove Smallest and Largest Elements from PHP Array In this article, we will learn how to remove the smallest and largest elements from an array in PHP. Handling arrays is a common task in PHP. We will cover various approaches to remove the smallest and largest elements from an array in PHP. Approach 1: Using sort() and array_shift()/array_pop() func 2 min read How to Remove First Element from an Array in PHP? Given an Array, the task is to remove the first element from the array in PHP. There are three methods to remove the first element from an array, these are - using array_shift(), array_slice(), and unset() functions. Let's explore each method with detailed explanations and code examples.Table of Con 3 min read How to Remove Last Element from an Array in PHP ? Removing the last element from an array is a common operation in PHP. There are several approaches to achieve this. In this article, we will explore different methods to remove the last element from an array.Table of ContentUsing array_pop() FunctionUsing array_slice() FunctionUsing unset() Function 3 min read Like