compare two JSON objects array and print specific content in php

Deadly 提交于 2020-04-17 20:22:31

问题


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
}

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

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

Here is the php code:

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

Problem Statement:

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

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.

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.

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.

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.

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


}} ?>

回答1:


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>

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>

Demo on 3v4l.org



来源:https://stackoverflow.com/questions/60101194/compare-two-json-objects-array-and-print-specific-content-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!