php - Return array key and value from ordered number -


i want return both key , value of array item, knowing numerically ordered number.

is there better method using these 2 functions?

$num = '3'; $array = [         'fish' => 'blue',         'monkey' => 'green',         'pig' => 'blue',         'cat' => 'yellow', ];  echo array_values($array)[$num]; // yellow echo array_keys($array)[$num]; // cat 

sure, array_slice()

$num = '3'; $array = [         'fish' => 'blue',         'monkey' => 'green',         'pig' => 'blue',         'cat' => 'yellow', ];   $newarray = array_slice($array, $num, 1); var_dump($newarray); 

works associative arrays


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -