比较两个JSON对象数组并在php中打印特定内容 [英] compare two JSON objects array and print specific content in php

查看:71
本文介绍了比较两个JSON对象数组并在php中打印特定内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have JSON objects array as shown below. The following content is in the file (feeds/ptp-ess_landing_house.json) mentioned at Line 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)值 strong>与之相关。

For every particular date there is a value (yes/no/nada) associated with it.

当月($ data_house-> house_sitting_date_current_month) 2月1日 2月5日 2月6日具有(其余均为nada)。


下个月<​​strong>($ data_house-> house_sitting_date_next_month); 3月2日 3月3日具有 3月1日(其余均为nada)。

For the current month ($data_house->house_sitting_date_current_month); Feb 1st, Feb 5th and Feb 6th have yes (rest all are nada).

For the next month ($data_house->house_sitting_date_next_month); March 2nd and March 3rd have yes. March 1st is No (rest all are 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>

问题陈述:

目前,我已经在C行对休息日进行了硬编码

At present I have hard-coded Sitting Day at Line C

我想知道应该在php代码中进行哪些更改在(特别是在C行)上方,以便 B行查找/匹配/扫描($ data_house-> house_sitting_date_current_month, )表示JSON中
以上的日期,并根据今天的日期 JSON

I am wondering what changes I should make in the php code above (specially at Line C) so that Line B looks/match/scan ($data_house->house_sitting_date_current_month, $data_house->house_sitting_date_next_month) for a date inside the JSON above and print content at Line C on the basis of today's date in the JSON.

案例1:如果今天的日期是 2020-02-1 > B行及其在JSON中对应日期的,然后应该在 C行说出就坐日。

Case 1: If today's date is 2020-02-1 at Line B and its yes for the corresponding date in the JSON, then it should say Sitting Day at Line C.

案例2::如果今天的日期是B行的 2020-03-01 ,而其对应日期的在t JSON,那么它应该说 C行不是就坐日。

Case 2: If today's date is 2020-03-01 at Line B and its no for the corresponding date in the JSON, then it should say Not a Sitting Day at Line C.

案例3:如果是今天的日期是 B行中的 2020-03-06 ,并且该特定日期的JSON中存在 nada ,则应显示空白/不显示在 C行

Case 3: If today's date is 2020-03-06 at Line B and nada is present in the JSON for that particular date, then it should say display blank/nothing at Line C.

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

I think, we need to use two foreach loops but more need to be done.

<?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) {


}} ?>


推荐答案

您需要在 $ data_house 中的两个就坐日期数组,使用 array_search ,然后使用该键确定房子是否在坐(或是否没有信息)。可以用来生成要在HTML中输出的字符串:

You need to search for your date of interest in the two sitting date arrays in $data_house using array_search, and then use that key to decide if the house is sitting or not (or if there is no information). That can be used to generate a string to output in the 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-02-06):

Output (for today, 2020-02-06):

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

在3v4l.org上的演示

这篇关于比较两个JSON对象数组并在php中打印特定内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆