Current practice: {code...} is there a simple method? Current practice:
# Join array $ a = ['A' => 1 ,... 'B' => 2]; $ first = reset ($ a); $ first_key = key ($ a); $ last = end ($ ); $ last_key = key ($ B );
Is there a simple way?
Reply content:
Current practice:
# Join array $ a = ['A' => 1 ,... 'B' => 2]; $ first = reset ($ a); $ first_key = key ($ a); $ last = end ($ ); $ last_key = key ($ B );
Is there a simple way?
If you obtain the key and value respectively, the topic can also obtain the fisrt method simply:
$first = reset($a);
If you want to obtain both the key and value at once, you can use each:
list($first_key, $first) = (reset($a) ? each($a) : each($a));list($last_key, $last) = (end($a) ? each($a) : each($a));
Ps: Unfortunately, PHP does not support comma expressions. Otherwise, you can directly write it like this:
list($first_key, $first) = (reset($a) , each($a));list($last_key, $last) = (end($a) , each($a));
Supplement: the list can be nested, so you can also write it like this:
list(,list($first_key, $first)) = array(reset($a) , each($a));list(,list($last_key, $last)) = array(end($a) , each($a));
php
print_r(current(array_keys($a)));//aprint_r(current(array_values($a)));//1print_r(end(array_keys($a)));//bprint_r(end(array_values($a)));//2