Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果未找到,php循环将显示当前年份数据,而不是跳过_Php_Loops_Foreach - Fatal编程技术网

如果未找到,php循环将显示当前年份数据,而不是跳过

如果未找到,php循环将显示当前年份数据,而不是跳过,php,loops,foreach,Php,Loops,Foreach,因此,我在一个循环中有一个循环,两个循环都有年份数据,我试图找出如果年份不匹配或循环中不存在年份,如何显示当前年份(2021年)数据,而不是使用“继续”跳过条目,任何想法或解决方案都值得赞赏 实时代码: 这就是我得到的输出: Array ( [0] => Array ( [year] => 2021 [price] => 255 ) ) Array ( ) Array ( [0]

因此,我在一个循环中有一个循环,两个循环都有年份数据,我试图找出如果年份不匹配或循环中不存在年份,如何显示当前年份(2021年)数据,而不是使用“继续”跳过条目,任何想法或解决方案都值得赞赏

实时代码:

这就是我得到的输出:

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

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

)
这就是我想要的结果:

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

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

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

)
试试这个

    $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'=>'2022'],
        ['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);
}
试试这个

    $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'=>'2022'],
        ['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);
}

我不确定我是否理解正确,但接下来

在循环之前初始化
$matched
变量,如果存在匹配项,则将其设置为
true
(以知道不需要添加当前年份数据)

如果没有匹配项,请搜索数据数组(
$rates\u results
)以查看当前年份是否存在,如果存在,请将其添加到结果中

<?php
$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) {
        $matched = false;
        
        foreach ($rates_results as $rates) {
            if(date("Y", strtotime($start_date)) !== ''.$rates['year'].'') {
                continue;
            }
            
            $matched = true;
            $rateIDs[] = [
                'year' => $rates['year'],
                'price' => $rates['price']
            ];
        }
        
        if ($matched === false) {
            $key = array_search(date('Y'), array_column($rates_results, 'year'));
            if ($key !== false) {
                $rateIDs[] = [
                    'year' => $rates_results[$key]['year'],
                    'price' => $rates_results[$key]['price']
                ];
            }
        }
    }
    print_r($rateIDs);
}

我不确定自己是否理解正确,但接下来

在循环之前初始化
$matched
变量,如果存在匹配项,则将其设置为
true
(以知道不需要添加当前年份数据)

如果没有匹配项,请搜索数据数组(
$rates\u results
)以查看当前年份是否存在,如果存在,请将其添加到结果中

<?php
$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) {
        $matched = false;
        
        foreach ($rates_results as $rates) {
            if(date("Y", strtotime($start_date)) !== ''.$rates['year'].'') {
                continue;
            }
            
            $matched = true;
            $rateIDs[] = [
                'year' => $rates['year'],
                'price' => $rates['price']
            ];
        }
        
        if ($matched === false) {
            $key = array_search(date('Y'), array_column($rates_results, 'year'));
            if ($key !== false) {
                $rateIDs[] = [
                    'year' => $rates_results[$key]['year'],
                    'price' => $rates_results[$key]['price']
                ];
            }
        }
    }
    print_r($rateIDs);
}

我认为您使用嵌套for循环太复杂了。实际上,您有一个
日期
数组和一个
费率
数组,您想将一个数组与另一个数组进行交叉引用吗

要做到这一点,您需要做的是循环遍历
日期
数组,然后使用PHPs
数组*
函数查找特定年份的密钥;然后,如果未发现任何内容,则使用当前年度

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

$rates_results = [
    ['price'=>255,'year'=>'2021'],
    ['price'=>300,'year'=>'2023']
];
$rate_year_array = array_column($rates_results, "year");             // Extract year "column" from $rates_results


foreach ($dates as $date_row) {
    $search_year  = date("Y", strtotime($date_row["date_starts"]));  // Get the year to look up from the date supplied
    $rate_key     = array_search($search_year, $rate_year_array);    // Search the "year" array and get the key for the current year
                                                                     // If not found then the value will be (bool) false
    
    if ($rate_key === false) {                                       // Check if the key was found
        $rate_key = array_search(date("Y"), $rate_year_array);       // If not find the key for the current year
    }

    // Output the date to an array
    $rateIDs[] = [
        'year'  => $rates_results[$rate_key]["year"],
        'price' => $rates_results[$rate_key]["price"],
    ];

}

print_r($rateIDs);  // Print the array
输出:

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

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

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

)

我认为使用嵌套for循环会使这个过程过于复杂。实际上,您有一个
日期
数组和一个
费率
数组,您想将一个数组与另一个数组进行交叉引用吗

要做到这一点,您需要做的是循环遍历
日期
数组,然后使用PHPs
数组*
函数查找特定年份的密钥;然后,如果未发现任何内容,则使用当前年度

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

$rates_results = [
    ['price'=>255,'year'=>'2021'],
    ['price'=>300,'year'=>'2023']
];
$rate_year_array = array_column($rates_results, "year");             // Extract year "column" from $rates_results


foreach ($dates as $date_row) {
    $search_year  = date("Y", strtotime($date_row["date_starts"]));  // Get the year to look up from the date supplied
    $rate_key     = array_search($search_year, $rate_year_array);    // Search the "year" array and get the key for the current year
                                                                     // If not found then the value will be (bool) false
    
    if ($rate_key === false) {                                       // Check if the key was found
        $rate_key = array_search(date("Y"), $rate_year_array);       // If not find the key for the current year
    }

    // Output the date to an array
    $rateIDs[] = [
        'year'  => $rates_results[$rate_key]["year"],
        'price' => $rates_results[$rate_key]["price"],
    ];

}

print_r($rateIDs);  // Print the array
输出:

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

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

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

)

从答案和对他们的评论来看,似乎你在寻找这样的东西:

$dates=[
[“开始日期”=>“2021-03-22”],
['date_Start'=>'2022-04-22'],
['date_Start'=>'2023-05-22']
];
$current_year=日期('Y');
foreach(日期为$dt){
$rates\u结果=[
['price'=>255,'year'=>2021'],
['price'=>200,'year'=>2021'],
['price'=>300,'year'=>2023']
];
//从开始日期开始获取年份。因为它是YYYY-MM-DD格式
//我们可以用substr
$start\u year=substr($dt['date\u start'],0,4);
//在$rates\u结果中查找匹配年份
$rate_keys=数组_keys(数组_列($rates_results,'year'),$start_year);
//有火柴吗?
如果(空($rate_key)){
//否,请改用本年度的值
$rate_keys=数组_keys(数组_列($rates_results,'year'),$current_year);
}
//得到实际利率
$code=1;
$rates=array();
foreach($按$key评级){
$rates[]=[
“自定义”=>$code,
“价格”=>$rates\u结果[$key]['price'],
“年”=>$rates\u结果[$key][“年”]
];
}
//输出速率
印刷费(费率);
}
输出:

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

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

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

)
数组
(
[0]=>阵列
(
[价格]=>255
[年份]=>2021年
)
[1] =>阵列
(
[价格]=>200
[年份]=>2021年
)
)
排列
(
[0]=>阵列
(
[价格]=>255
[年份]=>2021年
)
[1] =>阵列
(
[价格]=>200
[年份]=>2021年
)
)
排列
(
[0]=>阵列
(
[价格]=>300
[年份]=>2023年
)
)

从答案和对他们的评论来看,似乎你在寻找这样的东西:

$dates=[
[“开始日期”=>“2021-03-22”],
['date_Start'=>'2022-04-22'],
['date_Start'=>'2023-05-22']
];
$current_year=日期('Y');
foreach(日期为$dt){
$rates\u结果=[
['price'=>255,'year'=>2021'],
['price'=>200,'year'=>2021'],
['price'=>300,'year'=>2023']
];
//从开始日期开始获取年份。因为它是YYYY-MM-DD格式
//我们可以用substr
$start\u year=substr($dt['date\u start'],0,4);
//在$rates\u结果中查找匹配年份
$rate_keys=数组_keys(数组_列($rates_results,'year'),$start_year);
//有火柴吗?
如果(空($rate_key)){
//否,请改用本年度的值
$rate_keys=数组_keys(数组_列($rates_results,'year'),$current_year);
}
//得到实际利率
$code=1;
$rates=array();
foreach($按$key评级){
$rates[]=[
“自定义”=>$code,
“价格”=>$rates\u结果[$key]['price'],
“年”=>$rates\u结果[$key][“年”]
];
}
//输出速率
印刷费(费率);
}
输出:

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

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

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

)
数组
(
[0]=>阵列
(
[价格]=>255
[年份]=>2021年
)
[1] =>阵列
(
[价格]=>200
[年份]=>2021年
)
)
排列
(
[0]=>阵列
(
[价格]=>255
[年份]=>2021年
)
[1] =>阵列
(
[价格]=>200
[年份]=>2021年
)
)
排列
(
[0]=>阵列
(
[价格]=>300
[年份]=>2023年
)
)

在示例中,我给出了一个简单的示例,但原始代码要复杂一点,如果我使用嵌套for循环,您是否有可能也给出一个示例?在示例中,我给出了一个简单的示例,但原始代码要复杂一点,您是否有可能也给出一个示例