When I wrote PHP today, I encountered an array problem.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.