Intercepting qualified arrays-php Tutorial

Source: Internet
Author: User
Returns an array that meets the conditions.
$arr = Array(    6 => Array        (            id=> 358            level=> 6        )    4=> Array        (            id => 354            level => 4        )    5 => Array        (            id => 353            level => 5        ))

There is the order of such an array key to capture the appropriate array
If the key is sorted by 6 in the preceding column, for example, the key is sorted by 6 and 4 in the preceding column
$ Arr = Array
(
6 => Array
(
Id = & gt; 358
Level => 6
)
}
If the array is sorted as 5, 4, and 6, it takes only 5, 6.
$ Arr = Array
(
5 => Array
(
Id = & gt; 353
Level => 5
)
6 => Array
(
Id = & gt; 358
Level => 6
)
)
If 4 is sorted as 4, 5, and 6,
$ Arr = Array
(
4 => Array
(
Id = & gt; 354
Level => 4
)
5 => Array
(
Id = & gt; 353
Level => 5
)
6 => Array
(
Id = & gt; 358
Level => 6
)
)
If there are more than 4, 5, 6, 7, 8, 9, how can we optimize the above?


Reply to discussion (solution)

6, 4, 5, and 6 (the first or last one can be considered)
In the order of 5, 4, and 6, we can only take 5, 6 (this is to take both ends)
Order to 4, 5, and 6 (rules changed again)
A variable rule like yours cannot write programs.

You haven't provided any code, so optimization is even more difficult.

After reading this, your only rule is to take the value up based on the key of the first data.

5, 4, 6, the first is 5, get ratio 5 (including 5), is 5, 6
4, 5, 6, the first is 4, take the ratio of 4 (including 4), that is, 4, 5, 6

Then you record the key value of the first data during the loop. each time you determine whether the key is greater than the first value, it is recorded if it is greater
In this way, you don't need to worry about him, whether it's 7, 8, 9 or more data.

After reading this, your only rule is to take the value up based on the key of the first data.

5, 4, 6, the first is 5, get ratio 5 (including 5), is 5, 6
4, 5, 6, the first is 4, take the ratio of 4 (including 4), that is, 4, 5, 6

Then you record the key value of the first data during the loop. each time you determine whether the key is greater than the first value, it is recorded if it is greater
In this way, you don't need to worry about him, whether it's 7, 8, 9 or more data.


It's not disturbing. it's better than the first one. if it's OR 5, it's just. it seems there's no rule.

Consider listing all possible values.
Check whether the array_intersect_key fetch rule can be met.

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.