How to gracefully modify a key value when iterating through an array

Source: Internet
Author: User
$start = 6;$list = [    '1楼', '2楼', '3楼', '4楼', '5楼',];

How to add a value to a key in an array when traversing $start

Since it is now the Ajax load comment, the floor in the HTML code passed to the front end is from 1-5
I think the second time I asked for it, the number of floors is 6, 7, 8, 9, 10

The two functions on the label can only be referenced to the value, not to the key. So there's no other way to handle this. In the foreach case of no use.

PS: I now think of the use of array_keys all the keys out of the processing, and in the original array to merge

Reply content:

$start = 6;$list = [    '1楼', '2楼', '3楼', '4楼', '5楼',];

How to add a value to a key in an array when traversing $start

Since it is now the Ajax load comment, the floor in the HTML code passed to the front end is from 1-5
I think the second time I asked for it, the number of floors is 6, 7, 8, 9, 10

The two functions on the label can only be referenced to the value, not to the key. So there's no other way to handle this. In the foreach case of no use.

PS: I now think of the use of array_keys all the keys out of the processing, and in the original array to merge

$list = array_flip( array_map(function($key) use ($start) { return $start + (int)$key; }, array_flip($list)) );

Https://3v4l.org/O8rXf

$key_list = range($start, $start + count($list) - 1);$list     = array_combine($key_list, $list);
  • 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.