How to find the value in an auxiliary array and find out the key index of this auxiliary array

I have an array with this structure:

$months = array(
     MM => array(
         'start' => DD, 
         'end'   => DD,
         'type'  => (string),
         'amount'=> (float),
      ),
 );

MM - month (01-12, line), DD day in the month (01-31, line). Not all months may be in the array. For each month, there is a variable number of subbands with ranges with unique days each. For example, a month has three sub-ranges with three ranges of days, but the days used in these ranges will never overlap or duplicate, each DD value is unique. With one exception, in some ranges the "start" and "end" may coincide (the same day DD), but there will never be two identical "start" days or two identical "end" days for each month.

I need to use this array, looping months and days for each month. Cycle every day in the month, I have to check whether this particular day corresponds to "start" or "end". If the match is true, I also need to get adjacent values.

However, I ran into one problem: how can I find out the key index of a subrange, where is such a match? For example, how do you know if a match matches

$months['09'][3]['start'] == $current_day_in_loop;

or rather:

$months['09'][6]['start'] == $current_day_in_loop;

or another key?

Since I don’t know how many ranges exist for each month, the index key is variable or not at all. How to determine if a matching value is found with a key [3]or [6]? As soon as I know the key, I can use it to search for adjacent values ​​in the same submatrix.

+4
3

, , :

$matches = array_filter($months['09'], function($item) use ($current_day_in_loop) {
    return $item['start'] == $current_day_in_loop;
});
// if $matches is empty, there were no matches, etc.
foreach ($matches as $index => $item) {
    // $months['09'][$index] is the item as well
}
+3

:

//loop start
    if (($key = array_search($searchedVal, $dataArr)) !== false) {
        echo $key;
    }
// loop end
+1

, . : , ? , ,

, , , - , , , , .

, next

$months = array(
     '01' => array(
         'start' => '1', 
         'end'   => '31',
         'type'  => 'hello',
         'amount'=> 2.3,
      ),
     '02' => array(
         'start' => '2', 
         'end'   => '31',
         'type'  => 'best',
         'amount'=> 2.5,
      ),
     '03' => array(
         'start' => '3', 
         'end'   => '31',
         'type'  => 'test',
         'amount'=> 2.4,
      ),       
 );

$matches = array();
$prev = null;
$prev_key = null;
$key = key($months);
$month = reset($months);

while($month) {

    $next = next($months);
    $next_key = key($months);

    foreach(range(1,31) as $current_day_in_loop) {
        //if end or start match grab the current, previous and next values
        if($month['start'] == $current_day_in_loop
        || $month['end'] == $current_day_in_loop) {
            $matches[$key] = $month;

            if($prev)
                $matches[$prev_key] = $prev;

            if($next)
                $matches[$next_key] = $next;
        }
    }

    $prev = $month;
    $prev_key = $key;
    $month = $next;
    $key = $next_key;
}

print_r($matches);
+1

Source: https://habr.com/ru/post/1548582/


All Articles