I am trying to search multidimensional array but I get alwayes empty result. Here is my code:
public $tenant;
$this->tenant = array(
"store" => "grocery1",
array(
"station" => "st1",
"times" => array(
"day" => "Monday",
"from1" => "8:00",
"to1" => "12:00",
"from2" => "13:00",
"to2" => "17:30"
),
array(
"day" => "Wednesday",
"from1" => "8:00",
"to1" => "13:00",
"from2" => "",
"to2" => ""
),
array(
"day" => "Friday",
"from1" => "8:00",
"to1" => "12:00",
"from2" => "13:00",
"to2" => "20:00"
),
array(
"day" => "Saturdays",
"from1" => "10:00",
"to1" => "13:00",
"from2" => "",
"to2" => ""
),
)
);
$key = array_search(
"8:00",
array_column($this->tenant, 'from1')
);
echo $key;
The only element in the [0][‘times’] array is the first set (Monday) data. The rest of the sets are at indexes [0][0], [0][1], and [0][2].
I suspect you wanted to build the array as -
$this->tenant = array(
"store" => "grocery1",
array(
"station" => "st1",
"times" => array(
array("day" => "Monday",
"from1" => "8:00",
"to1" => "12:00",
"from2" => "13:00",
"to2" => "17:30"
),
array(
"day" => "Wednesday",
"from1" => "8:00",
"to1" => "13:00",
"from2" => "",
"to2" => ""
),
array(
"day" => "Friday",
"from1" => "8:00",
"to1" => "12:00",
"from2" => "13:00",
"to2" => "20:00"
),
array(
"day" => "Saturdays",
"from1" => "10:00",
"to1" => "13:00",
"from2" => "",
"to2" => ""
),
)
)
);
You would then need to use the following array_column statement to access the values -
array_column($this->tenant[0]['times'], 'from1')
1 Like
igor_g
August 19, 2020, 8:54pm
3
“from1” is column of $this->tenant[0].
1 Like
Thanks but I still get empty search result
How exactly are you coming up with this array?
I am trying to use data for stores open hours to work on it
Not helpful. What is the source of this data and how does it end up in this array?
If you are getting a zero (0), that’s the key of the 1st ‘8:00’ in the data. If you are getting a false (which would not display when echoed), that would mean that the value wasn’t found.
source is example documentation. for example I have many stores and store 1 has these open hours, store 2 has another different open hours so I am the person who made this array to use it on some operations like decide open and close times.
The array is not constructed properly. Can you provide a link to the documentation you mention?
use it on some operations like decide open and close times
Can you please provide more details on this.
1 Like
Here is the instructions:
Mondays, Tuesdays, and Thursdays from 8:00–12:00 and 13:00–17:30 Wednesdays from 8:00–13:00 Fridays from 8:00–12:00 and 13:00–20:00 Saturdays from 10:00–13:00
I need to send a time and get back if this store is closed or opened
We are getting there…
Is this to determine if a store is open for the current date and time or checking some random day and time? What is the high level overview of what you are doing?
1 Like
Thanks. I just need to send a timestamp with date and time and I need to return open or close.
I was able to fix it to get the key now. But if I did this array in wrong way please advise.
Start here…
* Take note: Timezone is hard coded for Los Angeles
<?php
function storeIsOpen() {
$status = FALSE;
$storeSchedule = [
'Mon' => ['08:00 AM' => '05:00 PM'],
'Tue' => ['08:00 AM' => '05:00 PM'],
'Wed' => ['08:00 AM' => '05:00 PM'],
'Thu' => ['08:00 AM' => '05:00 PM'],
'Fri' => ['08:00 AM' => '05:00 PM']
];
//get current West Coast US time
$timeObject = new DateTime('America/Los_Angeles');
$timestamp = $timeObject->getTimeStamp();
$currentTime = $timeObject->setTimestamp($timestamp)->format('H:i A');
// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// create time objects from start/end times and format as string (24hr AM/PM)
$startTime = DateTime::createFromFormat('h:i A', $startTime)->format('H:i A');
$endTime = DateTime::createFromFormat('h:i A', $endTime)->format('H:i A');
// check if current time is within the range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = TRUE;
break;
}
}
return $status;
}
echo storeIsOpen() ? 'Store Open' : 'Store Closed';
1 Like
Hint: For multiple stores, pass the hours array as a parameter to the function like so…
function storeIsOpen($storeSchedule)
echo storeIsOpen($storeSchedule1) ? 'Store Open' : 'Store Closed';
echo storeIsOpen($storeSchedule2) ? 'Store Open' : 'Store Closed';
echo storeIsOpen($storeSchedule3) ? 'Store Open' : 'Store Closed';
1 Like
system
Closed
November 19, 2020, 5:04pm
19
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.