Change language

Reset keys of array elements using PHP?

|
The built-in functions array_keys() and array_values ​​() can be used to access keys and values ​​of an associative array using an integer index.
  • The array_keys()function takes an input array and returns an indexed array of only the keys from the input array.
  • The array_values ​​()function takes an input array and returns an indexed array only the values ​​from the input array.
Now you can perform two operations to reset the keys of the array elements: key_swap() and key_change(). Since these are not built-in functions, they must be implemented in code.Using the key_swap() function:this function will inject an array and two keys and then change the values ​​corresponding to those two keys using another variable ($val) and return the resulting array.Note.This function will throw an error if both keys are missing in the array.Using the key_change() function:this function will 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 the third variable ($val), and then remove (unset()) the old key and its corresponding value from the array. The new key will then be added to the array, assigned the value stored in the third variable ($val), and the resulting array will be returned.Note.This function will return the desired result, if the new key is not present in the array, otherwise, if the new key is present in the input array, then the value of the new key will be lost as the value of the old key will overwrite it. The function will also throw an error if the old key is not present in the input array.Program:PHP program for resetting the keys of an array element in an array.
// PHP program for resetting array element keys 
// Function to exchange the values ​​of any
// two key in the array function key_swap ( $arr , $key1 , $key2 ) { $val = $arr [ $key1 ]; $arr [ $key1 ] = $arr [ $key2 ]; $arr [ $key2 ] = $val ; return $arr ; }  
// Function to change the key that matches
// to the value in the array function key_change ( $arr , $oldkey , $newkey ) { $val = $arr [ $oldkey ]; unset ( $arr [ $oldkey ]); $arr [ $newkey ] = $val ; return $arr ; }  
// An example of an associative array $arr = array ( ’zero’ = > 1, ’one’ = > 2, ’two’ = > 0, ’ test’ = > 3 );  
// Print array of samples echo "The Sample array:" ; print_r ( $arr );  
// Swap the "zero" and "one" keys $arr = key_swap ( $arr , ’zero’ , ’one’ );  
// Swap the "zero" and "two" keys $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 );  
?>
Exit:
 The Sample array : Array ([zero] = > 1 [one] = > 2 [two] = > 0 [test] = > 3) The Modified array: Array ([zero] = > 0 [one] = > 1 [two] = > 2 [three] = > 3) 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method