How to get first value from the array
How to get first value from the array
1 Answer
There are many ways to get the first array value based on the array type
If you have indexed array you can use
array_values() function
array_values() used to re-index array. then you can access the first element with index 0
Example
<?php
$my_index_array = [0=>'One',1=>'Two',4=>'Three'];
$my_index_array=array_values($my_index_array);
echo(($my_index_array[0]));
?>
Output
One
If you have Associative arrays you can use
array_keys() Function
array_keys() used to get all keys of the array then you can access the first array value with index $keys[0]
Example
<?php
$my_index_array = ['One'=>1,'Two'=>2,'Three'=>3];
$keys = array_keys($my_index_array);
echo $my_index_array[$keys[0]];
?>
Output
1
answer Link