Skip to content
Advertisement

php loop show current year data if not found instead of skiping

So I have a loop within a loop both have year data and I’m trying to figure out how to show current year (2021) data if years don’t match or exist on loop instead of using continue to skip the entry, any ideas or solutions are appreciated.

LIVE CODE: http://sandbox.onlinephpfunctions.com/code/b7cbef0613636aa41a41a7d5f283a2569ece9cb0

$dates = [
    ['date_starts'=>'2021-03-22'],
    ['date_starts'=>'2022-04-22'],
    ['date_starts'=>'2023-05-22']
];
              
foreach( $dates as $dt ){
    $start_date = $dt['date_starts'];
    
    $rates_results = [
        ['price'=>255,'year'=>'2021'],
        ['price'=>300,'year'=>'2023']
    ];

    $rateIDs = [];
    if ($rates_results) {
        foreach ($rates_results as $rates) {
            if(date("Y", strtotime($start_date)) !== ''.$rates['year'].''){
                continue;
            }
            
            $rateIDs [] = [
                'year' => $rates['year'],
                'price' => $rates['price']
            ];
        }
    }
    print_r($rateIDs);
}

So this is the OUTPUT I get:

Array
(
    [0] => Array
        (
            [year] => 2021
            [price] => 255
        )

)
Array
(
)
Array
(
    [0] => Array
        (
            [year] => 2023
            [price] => 300
        )

)

And this is the result I’m looking for:

Array
(
    [0] => Array
        (
            [year] => 2021
            [price] => 255
        )

)
Array
(
    [0] => Array
        (
            [year] => 2021
            [price] => 255
        )

)
Array
(
    [0] => Array
        (
            [year] => 2023
            [price] => 300
        )

)

Advertisement

Answer

Judging from the answers and the comments to them, it seems perhaps you are looking for something like this:

$dates = [
    ['date_starts'=>'2021-03-22'],
    ['date_starts'=>'2022-04-22'],
    ['date_starts'=>'2023-05-22']
];

$current_year = date('Y');
              
foreach ($dates as $dt) {
    
    $rates_results = [
        ['price'=>255, 'year'=>'2021'],
        ['price'=>200, 'year'=>'2021'],
        ['price'=>300, 'year'=>'2023']
    ];
    
    // get the year from the start date. Since it's in YYYY-MM-DD format
    // we can just use substr
    $start_year = substr($dt['date_starts'], 0, 4);
    
    // find matching years in $rates_results
    $rate_keys = array_keys(array_column($rates_results, 'year'), $start_year);
    
    // any matches?
    if (empty($rate_keys)) {
        // no, use the values from the current year instead
        $rate_keys = array_keys(array_column($rates_results, 'year'), $current_year);
    }
    
    // get the actual rates
    $code = 1;
    $rates = array();
    foreach ($rate_keys as $key) {
        $rates[] = [
            'custom' => $code,
            'price' => $rates_results[$key]['price'],
            'year' => $rates_results[$key]['year']
            ];
    }
    
    // output the rates
    print_r($rates);
}

Output:

Array
(
    [0] => Array
        (
            [price] => 255
            [year] => 2021
        )

    [1] => Array
        (
            [price] => 200
            [year] => 2021
        )

)
Array
(
    [0] => Array
        (
            [price] => 255
            [year] => 2021
        )

    [1] => Array
        (
            [price] => 200
            [year] => 2021
        )

)
Array
(
    [0] => Array
        (
            [price] => 300
            [year] => 2023
        )

)

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement