How to elegantly modify the key value when traversing an array-php Tutorial

Source: Internet
Author: User
{Code ...} how to add the $ start value to the key value in the array during traversal because ajax loads comments now, therefore, the floors in the html code passed to the front-end are from 1 to 5. when I want to make the second request, the number of floors is 6, 7, 8, 9, and 10. I saw that the two functions on the tag can only be set to values. reference...
$ Start = 6; $ list = ['first floor ', 'Second Floor', 'third floor ', 'fourth floor', 'fifth floor ',];

How to add the key value in the array during traversal$startValue

Because ajax loads comments, the floors in the html code sent to the front end are from 1 to 5.
The number of floors in the second request is 6, 7, 8, 9, and 10.

The two functions on the tag can only reference the value, but cannot reference the key. So is there any other way to handle it. Noforeach.

PS: What I think of now is to usearray_keysAfter all the keys are obtained and processed, they are merged with the original array.

Reply content:
$ Start = 6; $ list = ['first floor ', 'Second Floor', 'third floor ', 'fourth floor', 'fifth floor ',];

How to add the key value in the array during traversal$startValue

Because ajax loads comments, the floors in the html code sent to the front end are from 1 to 5.
The number of floors in the second request is 6, 7, 8, 9, and 10.

The two functions on the tag can only reference the value, but cannot reference the key. So is there any other way to handle it. Noforeach.

PS: What I think of now is to usearray_keysAfter all the keys are obtained and processed, they are merged with the original array.

$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);
Related Article

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.