Searching inside an array

I have an array which contains weekdays and for each day associating hours when a
store open and closes(JSON format follows):

{"sunday":{"open":null,"close":null},"id":{"id":8},"monday":{"open":null,"close":null},"tuesday":{"open":null,"close":null},"wednesday":{"open":"08:30:00","close":"14:30:00"},"thursday":{"open":"11:00:00","close":"17:00:00"},"friday":{"open":null,"close":null},"saturday":{"open":null,"close":null}}

where you see NULL values above it means that at these days the store is closed.

The goal is that I want to create another array that will include all the days that the store is closed…how am I going to extract these data(days closed) from the array you see above…I think I should use a foreach loop but from there I do not know where to go.

Hi designtrooper,

First of all, (if it’s not already) you’ll want to convert your JSON into a native PHP array:

// We pass true as the second argument so we get associative arrays, not objects
$openingHours = json_decode($jsonArray, true);

then you can use array_filter to return an array containing only the days when the store is closed:

$closedDays = array_filter($openingHours, function($day) {
    return is_null($day['open']);
});

Basically, array_filter passes each item in the array to a function (in this case, an anonymous function), and if that function returns true then the value is returned as part of a new array.

before seeing your reply and in order to get the closed days I had concluded in this code:

function get_closed_days($value,$array){
$cl_days=array();
 foreach ($array as $key => $val) {
 if ($val['open'] === $value) {
       array_push($cl_days, $key);
    }
 
 }
 return $cl_days;
   }

And call it:

get_closed_days(NULL,$sched);

var_dump gives this(for example):

array(5) {


[0]=> string(6) "sunday" [1]=>string(6) "monday"[2]=>string(7) "tuesday"[3]=> string(6) "friday"[4]=>string(8) "saturday" }

We are done now with this…there is something left though…which I did not mention from the beginning so as to not complicate things.

I want to make a new array with these days but with the keys being 0-7…counting from sunday to saturday…for example friday must have a key of 5 and not 3 as seen above…I hope you understand what I mean.

@fretburner - your solution returns a lot more than just the required days closed.

array(6) { ["sunday"]=> array(2) { ["open"]=> NULL ["close"]=> NULL } ["id"]=> array(1) { ["id"]=> int(8) } ["monday"]=> array(2) { ["open"]=> NULL ["close"]=> NULL } ["tuesday"]=> array(2) { ["open"]=> NULL ["close"]=> NULL } ["friday"]=> array(2) { ["open"]=> NULL ["close"]=> NULL } ["saturday"]=> array(2) { ["open"]=> NULL ["close"]=> NULL } }

I am also getting a NOTICE that “open” isn’t a proper index for the array, which I am getting in designtrooper’s solution too and to be honest, I am not sure why myself.

@designtrooper - are you looking for a key->value pair with the week days and whether the store is open or closed? If yes, then don’t use array_push, just use the $cl_days[$key] = "closed" and continue adding to the array outside of the if statement with $cl_days[$key] = "open" .

I came up with this.

$newTimes = array();

$json = '{"sunday":{"open":null,"close":null},"id":{"id":8},"monday":{"open":null,"close":null},"tuesday":{"open":null,"close":null},"wednesday":{"open":"08:30:00","close":"14:30:00"},"thursday":{"open":"11:00:00","close":"17:00:00"},"friday":{"open":null,"close":null},"saturday":{"open":null,"close":null}}';

$dataArray = json_decode($json, true);

foreach ($dataArray as $day => $times) {
    foreach ($times as $time) {
        if ( $time === null ) {
            $newTimes[$day] = "closed";
        } elseif (is_string($time)) {
            $newTimes[$day] = "open";
        }
    }
}

I had to add the “is_string” because of the “id” being in the middle of the JSON, otherwise it could have been just an “else”. Why is the “id” in the middle of the JSON?

This is what I ended up with.

array(7) { ["sunday"]=> string(6) "closed" ["monday"]=> string(6) "closed" ["tuesday"]=> string(6) "closed" ["wednesday"]=> string(4) "open" ["thursday"]=> string(4) "open" ["friday"]=> string(6) "closed" ["saturday"]=> string(6) "closed" }

Scott

Ah, I’d not spotted that there is a key ‘id’ in among the days. As there is no key ‘open’ within the nested array, PHP is generating the NOTICE error.

no…let me try to be more specific…I use a calendar plugin where you can specify as an option the weekdays NOT appearing in the calendar…this option is an array from values 0 to 6.

That is why I am telling that the output of the function I have made get_closed_days
must be “matched” with the array option of the plugin.
In the output of the function you will notice for example that saturday has a key of 4.
This somehow must be mapped to an array which saturday will have key of 6.
And this last array will be used by the calendar plugin for specifying hidden days(the days in other words the store will be closed)

$newTimes = array();

$x = 0;

$json = '{"sunday":{"open":null,"close":null},"id":{"id":8},"monday":{"open":null,"close":null},"tuesday":{"open":null,"close":null},"wednesday":{"open":"08:30:00","close":"14:30:00"},"thursday":{"open":"11:00:00","close":"17:00:00"},"friday":{"open":null,"close":null},"saturday":{"open":null,"close":null}}';

$dataArray = json_decode($json, true);

foreach ($dataArray as $day => $times) {
    foreach ($times as $time) {
        if ( $time === null ) {
            $newTimes[$x] = "closed";
        } elseif (is_string($time)) {
            $newTimes[$x] = "open";
        } else {
            $x--;
        }
    }
    $x++;
}

results in

array(7) { [0]=> string(6) "closed" [1]=> string(6) "closed" [2]=> string(6) "closed" [3]=> string(4) "open" [4]=> string(4) "open" [5]=> string(6) "closed" [6]=> string(6) "closed" }

If you could formulate your JSON so that the id array isn’t in the middle, this could be a lot cleaner.

Scott

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.