0

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


}} ?>
6
  • What you mean with Line A, Line B and Line C? Commented Feb 6, 2020 at 18:29
  • 1
    Have you tried anything to achieve this desired result? You don't seem to have a "problem" statement so much as a requirement that you're wanting us to complete for you. Commented Feb 6, 2020 at 18:33
  • @PatrickQ I think two foreach loop will be use. I will show you what I have tried. Commented Feb 6, 2020 at 18:34
  • @Pipe Line, Line B and Line C is inside the code. Commented Feb 6, 2020 at 18:35
  • @PatrickQ Check my edit. Commented Feb 6, 2020 at 18:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Nick, I hope all is well. I have a question related to unicode and array_filter. I am wondering if you can have a look.
@flash Sorry, I'm going away for a couple of days. If it's not resolved when I come back and I'll take a look.
Thank you. No problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.