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
Post a Comment