Change language

How to access associative array by integer index in PHP?

|
Although array_keys() can be used to get an indexed array of keys for an associative array. Since the resulting array is indexed, the elements of the resulting array can be accessed by an integer index. Using this resulting array, the keys of the original array can be accessed by an integer index, and then the keys can be used to access the elements of the original array. Thus, using an integer index, you can access the elements of the original array using an optional indexed array of keys.array_keys() function:array_keys() function takes an array as input and returns an indexed array that only consists of the keys of the original array, indexed where indexing starts at zero.Syntax:
array array_keys ($arr )
Parameters:The array_keys() function takes an array as input and uses only the array keys to create the resulting indexed array.Note.The array_keys() function does not change the order of the keys in the original array. If an indexed array is passed, the resulting array will have integers as its value.Program:PHP program for accessing an associative array using an integer index.
// PHP program for accessing associative
// array by integer index 
/ / Example associative array $arr = array ( ’one’ = > ’engineer’ , ’two’ = > ’for’ , ’three’ = > ’engineer’ );  
// Getting keys from $arr // using the array_keys() function $keys = array_keys ( $arr ); echo " The keys array: " ; print_r ( $keys );  
// Get the size of the sample array $size = sizeof ( $arr );  
// Access $arr elements using
// integer index using $x echo "The elements of the sample array:" . "" ; for ( $x = 0; $x < $size ; $x ++) { echo "key:" . $keys [ $x ]. ", value:" . $arr [ $keys [ $x ]]. "" ; }  
?>
Exit:
The keys array : Array ([0] = > one [1] = > two [2] = > three) The elements of the sample array: key: one, value: engineer key: two, value: for key: three, value: engineer

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

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