Click here to Skip to main content
14,694,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a question how to solve a problem in PHP.

Goal is to create an array, that includes all data for all weekdays, separated by 30 minutes.

Some data is fetched from a SQL database into an array, that looks like that.
Array
(
    [0] => Array
        (
            [timekey] => 2020-08-06 01:00:00
            [0] => 2020-08-06 01:00:00
            [count] => 1
            [1] => 1
	    [day] => Monday
	    [2] => Monday
        )
    [1] => Array
        (
            [timekey] => 2020-08-06 01:30:00
            [0] => 2020-08-06 01:30:00
            [count] => 1
            [1] => 1
	    [day] => Monday
	    [2] => Monday
        )
.
.
.

So I need to add the missing values for the timestamps that are not part of the SQL result

In this example
Array
(
    [0] => Array
        (
            [timekey] => 2020-08-06 00:00:00
            [0] => 2020-08-06 00:00:00
            [count] => 0
            [1] => 0
	    [day] => Monday
	    [2] => Monday
        )
    [1] => Array
        (
            [timekey] => 2020-08-06 00:30:00
            [0] => 2020-08-06 00:30:00
            [count] => 0
            [1] => 0
	    [day] => Monday
	    [2] => Monday
        )
.
.
.

At the end of the day I must have an array for Monday to Sunday, separated with all times in 30 minutes sequence.

I hope this was clear.
Does anybody have a hint how I can solve this problem?

Thank you very much

Tobi

What I have tried:

Try to create the complete array for all weekdays and timestamps with count = 0 and doe aaray search for the values from the database
Posted
Updated 7-Aug-20 8:52am
v2
Comments
Richard MacCutchan 6-Aug-20 6:41am
   
Those time values do not look valid for PHP. How did you create them?
Member 14852988 11-Aug-20 9:02am
   
The values for times are the results of an SQL extraction.

1 solution

You need a strategy - for example, if you need an array with all timestamps 30min apart then create one with default entries (whatever those are in your scenario).

Next, when you retrieve your data from SQL you can just replace the default array data with the real data.

Now you don't want to loop through the array and test for values for each of the SQL entries - and you don't have to: your array entries at the level of the first index allow you to calculate which entry matches your incoming SQL for the replacement.


   
Comments
Member 14852988 11-Aug-20 9:10am
   
Thats my problem.
How do I replace them?

The default values will be created like this (I know, there is room for improvements)
$current_date = '2020-08-11 00:00:00';
$array2d[0]['date'] = $current_date;
$array2d[0]['count'] = '0';
$array2d[0]['day'] = 'Montag';

$i = 1;

while ($i <= 336) {

$newTime = date("Y-m-d H:i:s",strtotime("+30 minutes", strtotime($current_date)));
$current_date = $newTime;
$array2d[$i]['date'] = $current_date;
$array2d[$i]['count'] = '0';
$array2d[$i]['day'] = 'Monday';
if ($i >= 48) {
$array2d[$i]['day'] = 'Tuesday';
}
if ($i >= 96) {
$array2d[$i]['day'] = 'Wednesday';
}
if ($i >= 144) {
$array2d[$i]['day'] = 'Thursday';
}
if ($i >= 192) {
$array2d[$i]['day'] = 'Friday';
}
if ($i >= 240) {
$array2d[$i]['day'] = 'Saturday';
}
if ($i >= 288) {
$array2d[$i]['day'] = 'Sunday';
}

$i++;
}

Now the default value for field COUNT example "2020-08-11 17:30:00" for "Friday" should be replaced for the value found in the SQL statement for the given time and day.
W Balboos, GHB 11-Aug-20 10:21am
   
OK - then do just that: replace the dummy array entry, when you have a real one.

One thing you might want to do is to not have one big giant array for the whole week. Why not the same array for each day array and have each day in a member of the day-of-week array?

But you need to learn more about array handling in php - it's really easy but you absolutely MUST learn this!

Start here: https://www.w3schools.com/php/php_arrays.asp and don't miss the link to the various array functions (PHP Array Referenc) you can use along with just directly accessing members. https://www.w3schools.com/php/php_ref_array.asp

My style of responding is to not "give away the answer" but guide the user into to to get the answer - to lean - so they can do it themselves forever after,
Member 14852988 12-Aug-20 2:34am
   
Hi,

thank you for the hint.
I am not expection a walk trhough or the complete code. Often a hint what to search or what to read is much more helpfluff, than to copy a code.
I will have a look on it and try to find a way to solve it.

Thank you very much
W Balboos, GHB 12-Aug-20 8:05am
   
A long-term hint: if you give it proper thought, you can set up your numeric array indices so that they actually identify the data. This is particularly easy in php since you can declare an array element without having to create the intermediate ones. An example would be using the year as an index dimension, then at the next level, the month (etc.). This way, if you had something you needed to put into January 1984 you could have an array element: stuff[1984][1]

Since it is php, you don't need to have empty useless ones such as stuff[1492] but you could create it at any time if you need it.

The idea is the value gives the index - you don't have to search: it defines itself.

Don't forget: if my or some else's solution solves your problem mark the question answered.
Member 14852988 13-Aug-20 2:53am
   
Hi,

thank you for your answer. Thats a good idea and I tried it.
Its much more simpler.
I did a loop trhough the entries I found with the SQL.
In this I compared the time and the day with the entries in my default array. If it fits I set the the default array values to the values of the SQL result.
Now it is working.
Thank you for all your hints.

BR

Tobi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900