I have an array of objects, and I want to count the number of previous and next indexes from a given value, up to a certain maximum count.
Array
(
[0] => stdClass Object
(
[id] => 460
)
[1] => stdClass Object
(
[id] => 484
)
[2] => stdClass Object
(
[id] => 485
)
[3] => stdClass Object
(
[id] => 486
)
[4] => stdClass Object
(
[id] => 489
)
[5] => stdClass Object
(
[id] => 501
)
[6] => stdClass Object
(
[id] => 654
)
Case: maximum count is 2
- If the value is 460, the previous index count is 0 (as
[0]
) and the next index count is 2 (as[1]
and[2]
) - If the value is 485, the previous index count is 2 (as
[0]
and[1]
) and the next index count is 2 (as[3]
and[4]
) - If the value is 501, the previous index count is 2 (as
[3]
and[4]
) and the next index count is 1 (as[6]
) - If the value is 654, the previous index count is 2 (as
[4]
and[5]
) and the next index count is 0 (as[6]
)
How can I get these two counts when an array, a value and a maximum count is given?