代码之家  ›  专栏  ›  技术社区  ›  flash

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

  •  0
  • flash  · 技术社区  · 6 年前

    我有一个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_house->house_sition_date_current_month) ; 2月1日 , 2月5日 和 2月6日 有 对 (其余都是娜达)。

    下个月 ($data_house->house_sition_date_next_month) ; 3月2日 和 3月3日 有 对 . 3月1日 是 不 (其余都是娜达)。

    以下是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>
    

    问题陈述:

    目前我有硬编码 休息日 在C线

    我想知道我应该在上面的php代码中做些什么更改 (特别是C线) 因此 B行 外观/匹配/扫描 ($data_house->house_sitting_date_current_month, $data_house->house_sitting_date_next_month) 上面JSON中的日期 并在 C行 基于 今天的日期 在 JSON .

    案例1: 如果今天的日期是 2020-02-1 在 B行 以及它的 对 对于JSON中相应的日期,则应该是在 C行 .

    案例2: 如果今天的日期是 2020-03-01 在B线和它的 不 对于JSON中相应的日期,则应该说不是在 C行 .

    案例3: 如果今天的日期是 2020-03-06 在 B行 和 娜达 在特定日期的JSON中存在,那么应该在 C行 .

    我认为,我们需要使用两个foreach循环,但还需要做更多的工作。

    <?php foreach ($data_house->house_sitting_date_current_month as $key1 => $val1) {
        foreach ($data_house->house_sitting_date_yes_no_current_month as $key1 => $val1) {
    
    
    }} ?>
    
    
    <?php foreach ($data_house->house_sitting_date_next_month as $key2 => $val2) {
        foreach ($data_house->house_sitting_date_yes_no_next_month as $key2 => $val2) {
    
    
    }} ?>
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   Nick SamSmith1986    6 年前

    您需要在中的两个会议日期数组中搜索感兴趣的日期 $data_house 使用 array_search ,然后用那把钥匙决定房子是否还在(或者是否没有信息)。可用于生成要在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>
    

    产量(截至2020年2月6日):

    <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>
    

    Demo on 3v4l.org