In {code...} PHP, how can we efficiently retrieve the first 10 of these arrays and determine the number of keys whose key is 333.
array ( 22 => 3000, 1 => 2900, 8 => 2588, 11 => 2400, 6 => 2000, 30 => 1900, 39 => 1800, 25 => 1400, 40 => 1300, 100 => 1244, 15 => 1000, 69 => 900, 70 => 888, 3 => 666, 333 => 10 );
In PHP, how can such an array efficiently retrieve the first 10 and how can we calculate the number of keys with 333.
Reply content:
array ( 22 => 3000, 1 => 2900, 8 => 2588, 11 => 2400, 6 => 2000, 30 => 1900, 39 => 1800, 25 => 1400, 40 => 1300, 100 => 1244, 15 => 1000, 69 => 900, 70 => 888, 3 => 666, 333 => 10 );
In PHP, how can such an array efficiently retrieve the first 10 and how can we calculate the number of keys with 333.
3000, 1 => 2900, 8 => 2588, 11 => 2400, 6 => 2000, 30 => 1900, 39 => 1800, 25 => 1400, 40 => 1300,100 => 1244, 15 => 1000, 69 => 900, 70 => 888, 3 => 666,333 => 10 ); // use array_slice to extract the first 10 elements $ arr1 = array_slice ($ array, 0, 10, true); print_r ($ arr1 ); // obtain the location where the key is 333 through array_search $ pos = array_search (333, array_keys ($ array); echo $ pos + 1; // $ pos + 1 indicates the actual position, $ pos indicates the position in the array, and starts from 0.