比较两个JSON对象数组并在php中打印特定内容

比较两个JSON对象数组并在php中打印特定内容,php,arrays,json,date,Php,Arrays,Json,Date,我有一个JSON对象数组,如下所示。以下内容位于文件(feed/ptp-ess_landing_house.json)中,该文件在A行中提到 { "house_sitting_date_current_month": ["2020-02-01", "2020-02-02", "2020-02-03", "2020-02-04", "2020-02-05", "2020-02-06"], "house_sitting_date_yes_no_current_month": ["ye

我有一个JSON对象数组,如下所示。以下内容位于文件(feed/ptp-ess_landing_house.json)中,该文件在A行中提到

{
    "house_sitting_date_current_month": ["2020-02-01", "2020-02-02", "2020-02-03", "2020-02-04", "2020-02-05", "2020-02-06"],
    "house_sitting_date_yes_no_current_month": ["yes", "nada", "nada", "nada", "yes", "yes"],
    "house_sitting_date_next_month": ["2020-03-01", "2020-03-02", "2020-03-03", "2020-03-04", "2020-03-05", "2020-03-06"],
    "house_sitting_date_yes_no_next_month": ["no", "yes", "yes", "nada", "nada", "nada"],
    "toggle_status": null
}
对于每个特定日期,都有一个与之关联的值(是/否/nada)

当月($data\u house->house\u siting\u date\u current\u month)2月1日2月5日2月6日(其余均为nada)

下个月($data\u house->house\u siting\u date\u next\u月)三月二日三月三日三月一日为否(其余均为nada)

以下是php代码:

<?php

if (file_exists('feeds/ptp-ess_landing_house.json')) {
    $data_house = json_decode(file_get_contents('feeds/ptp-ess_landing_house.json'));  // Line A
}

$date = date("Y-m-d");

$sitting_day_str_en = "Sitting day";

$not_a_sitting_day_str_en ="Not a Sitting Day";


?>

<header class="entry-header container">
   <?php
      the_title('<h1 class="entry-title-house">', '</h1>');
    ?>
   <span class="current-date"><?php echo $date ?></span><!-- prints today's date--> // Line B 
         <?php if (ICL_LANGUAGE_CODE == 'en') { ?>        <!-- English -->
                <span class="current-date-answer">Sitting Day</span> // Line C                           
        <?php } ?>
</header>

您需要使用在
$data\u house
中的两个坐着日期数组中搜索您感兴趣的日期,然后使用该键确定房子是否坐着(或者是否没有信息)。可用于生成要在HTML中输出的字符串:

$date = date("Y-m-d");

$sitting_day_str_en = "Sitting day";

$not_a_sitting_day_str_en ="Not a Sitting Day";

if (($k = array_search($date, $data_house->house_sitting_date_current_month)) !== false) {
    $sitting = $data_house->house_sitting_date_yes_no_current_month[$k];
    $sitting_str_en = $sitting == 'yes' ? $sitting_day_str_en : ($sitting == 'no' ? $not_a_sitting_day_str_en : '');
}
elseif (($k = array_search($date, $data_house->house_sitting_date_next_month)) !== false) {
    $sitting = $data_house->house_sitting_date_yes_no_next_month[$k];
    $sitting_str_en = $sitting == 'yes' ? $sitting_day_str_en : ($sitting == 'no' ? $not_a_sitting_day_str_en : '');
}
else {
    // not found
    $sitting_str_en = 'No data available';
}

?>

<header class="entry-header container">
   <?php
      the_title('<h1 class="entry-title-house">', '</h1>');
    ?>
   <span class="current-date"><?php echo $date ?></span><!-- prints today's date--> // Line B 
         <?php if (ICL_LANGUAGE_CODE == 'en') { ?>        <!-- English -->
                <span class="current-date-answer"><?= $sitting_str_en ?></span> // Line C                           
        <?php } ?>
</header>
$date=日期(“Y-m-d”);
$SITING\u day\u str\u en=“SITING day”;
$not_a_seating_day_str_en=“not a seating day”;
if($k=数组搜索($date,$data\u house->house\u siting\u date\u current\u month))!==false){
$siting=$data\u house->house\u siting\u date\u yes\u no\u current\u month[$k];
$siting\u str\u en=$siting='yes'?$siting\u day\u stru en:($siting='no'?$not\u siting\u day\u stru en:);
}
elseif($k=数组搜索($date,$data\u house->house\u siting\u date\u next\u month))!==false){
$siting=$data\u house->house\u siting\u date\u yes\u no\u下个月[$k];
$siting\u str\u en=$siting='yes'?$siting\u day\u stru en:($siting='no'?$not\u siting\u day\u stru en:);
}
否则{
//找不到
$siting_str_en='无可用数据';
}
?>

你对A线、B线和C线的意思是什么?你试过什么来达到这个期望的结果吗?您似乎没有“问题”陈述,更没有要求我们为您完成的要求。@PatrickQ我认为每个循环将使用两个foreach循环。我将向您展示我所尝试的。@代码中有管道、B行和C行。@PatrickQ检查我的编辑。你好,尼克,我希望一切顺利。我有一个与unicode和数组相关的过滤器。我想知道你是否可以看一看。@flash对不起,我要离开几天。如果我回来的时候还没有解决,我会看一看。谢谢。没问题。
$date = date("Y-m-d");

$sitting_day_str_en = "Sitting day";

$not_a_sitting_day_str_en ="Not a Sitting Day";

if (($k = array_search($date, $data_house->house_sitting_date_current_month)) !== false) {
    $sitting = $data_house->house_sitting_date_yes_no_current_month[$k];
    $sitting_str_en = $sitting == 'yes' ? $sitting_day_str_en : ($sitting == 'no' ? $not_a_sitting_day_str_en : '');
}
elseif (($k = array_search($date, $data_house->house_sitting_date_next_month)) !== false) {
    $sitting = $data_house->house_sitting_date_yes_no_next_month[$k];
    $sitting_str_en = $sitting == 'yes' ? $sitting_day_str_en : ($sitting == 'no' ? $not_a_sitting_day_str_en : '');
}
else {
    // not found
    $sitting_str_en = 'No data available';
}

?>

<header class="entry-header container">
   <?php
      the_title('<h1 class="entry-title-house">', '</h1>');
    ?>
   <span class="current-date"><?php echo $date ?></span><!-- prints today's date--> // Line B 
         <?php if (ICL_LANGUAGE_CODE == 'en') { ?>        <!-- English -->
                <span class="current-date-answer"><?= $sitting_str_en ?></span> // Line C                           
        <?php } ?>
</header>
<header class="entry-header container">
   <h1 class="entry-title-house">House Sitting Days</h1>   <span class="current-date">2020-02-06</span><!-- prints today's date--> // Line B 
                 <!-- English -->
                <span class="current-date-answer">Sitting day</span> // Line C                           
        </header>