如何解决php循环显示当前年份数据,如果没有找到而不是跳过
所以我有一个循环中的循环都有年份数据,我试图弄清楚如果年份不匹配或存在于循环中而不是使用继续跳过条目,则如何显示当前年份(2021)数据,任何想法或解决方案表示赞赏。
实时代码: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);
}
这是我得到的输出:
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
)
)
解决方法
我认为你把这个嵌套的 for 循环变得过于复杂了。实际上,您有一个 dates
数组和一个 rates
数组,并且您想将一个数组与另一个交叉引用?
为此,您需要做的是遍历 dates
数组,然后使用 PHP 的 array_*
函数查找特定年份的键;那么如果什么都没有找到,则使用当前年份。
$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
)
)
,
我不确定我是否理解正确,但是这里...
在循环之前初始化一个 $matched
变量,如果匹配则将其设置为 true
(知道不需要添加当前年份数据)。
如果没有匹配,则搜索数据数组($rates_results
)查看当前年份是否存在,如果存在,则将其添加到结果中
<?php
$dates = [
['date_starts'=>'2021-03-22'],['date_starts'=>'2023-05-22']
];
foreach( $dates as $dt ){
$start_date = $dt['date_starts'];
$rates_results = [
['price'=>255,'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);
}
,
从答案和对他们的评论来看,您似乎正在寻找这样的东西:
$dates = [
['date_starts'=>'2021-03-22'],['date_starts'=>'2023-05-22']
];
$current_year = date('Y');
foreach ($dates as $dt) {
$rates_results = [
['price'=>255,['price'=>200,'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'],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,$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);
}
输出:
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
)
)
,
试试这个
$dates = [
['date_starts'=>'2021-03-22'],'year'=>'2022'],'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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。