can't get my head round this php array

Joined
12 Feb 2006
Posts
17,348
Location
Surrey
i think i've just over complicated things for myself, but i'm struggling to figure out the way to store this in a php array.

essentially i am trying to display the total hours worked by shift staff, broken down by week. This is showing just 1 staff member at a time.

the data that is stored in a mysql database is the date + hours worked.

I first grab all the dates for x staff member, going back 12 weeks from todays date.

using a php while loop for each entry, I check what week number of the year that date is within.

I want to do the following:

if it's the same week as the last entry, add those hours to the total hour count for that week in an array and then store the date and hours so that later I can break down the array to show something like this.

Week 43 - Total Hours 7
12/10 - 4 hours
11/10 - 3 hours

Week 42 - Total hours 20
09/10 - 10 hours
06/10 - 6 hours
05/10 - 4 hours

Week 40 - Total Hours 2
22/09 - 2 hours

etc etc.

I just can't figure out how to store this in an array. The week isn't guaranteed to be worked within, and the amount of entries per week is different each week too.

I know i could just display the data as I do the while loop, however i want the count of total hours above the date/hours lines.
 
PHP:
$outputArray = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Date Variables
    $workedDate = new DateTime($row['workedDate']);
    $workedDateWeekNumber = $workedDate->format('W');
    $workedDateDMY = $workedDate->format('d/m/Y');

    // !Exist, Create
    if (!isset($outputArray[$workedDateWeekNumber])) {
        $outputArray[$workedDateWeekNumber] = array(
            [U][B]'totalHours' = 0;[/B][/U]
            'staffHours' = array();
        );
    }

    // Add Data
    $outputArray[$workedDateWeekNumber]['staffHours'] += array($workedDateDMY => $row['workedHours']);
    $outputArray[$workedDateWeekNumber]['totalHours'] += (int) $row['workedHours'];
}

thanks. this seems pretty much what i want. playing with it now

EDIT

worked a trick. it's storing the data perfectly, however my issue is now getting to use the data :p

I thought the below would work but it just says "Notice: Undefined offset: 0 "

PHP:
$keys = array_keys($outputArray);

for($i = 0; $i < count($outputArray); $i++) {
    echo "<tr>";
    echo "<td>".$outputArray[$keys[$i]][0]."</td>";
    echo "<td>".$outputArray[$keys[$i]][1]."</td>";
    echo "</tr>";
    
}
 
Last edited:
ok thanks guys. it's now all working, with 2 issues i've since realised.

one is minor. i used to also store the database row id for the times so that i could click each row to edit it, and also, the username of the person who submitted the time for each row. these are called:

$row['whoSubmit']
$row['timesId']

the bigger issue i've realised is that if 2 different times are added for the same date. this adds up fine for the total amount of times per week, and also gets stored in the array, but when the for loop goes round to display the data, it skips if there is more than 1 entry per date.
 
back again with this array mumbo jumbo

different issue but i'm sure easy to solve that i just can't figure out.

i have the below array. it used to have name, price, qty, total etc, but i've decided to remove that.

that now leaves the array looking simpler like this
PHP:
  $products_info=[

    ["name"=>"One-Off Clean"],
    ["name"=>"Oven Valet"],
    ["name"=>"Carpet Clean"],
    ["name"=>"Inside Windows"],
  ];

what i want to do is be able to simply add new lines to this array with some if statements, e.g.

PHP:
if ($quote_prop > 0) {
    $generalcleaning = true;
    $products_info += [ "name" => "General Cleaning" ];
}

nothing makes sense. nothing i read about adding to an existing array matches exactly what i'm after. i know i could simplify this array already, but it's part of something else that allows me to have more options in the future, and if i changed this array i'd need to change that code too, and i'd rather keep it as is for now and leave the ability for it to come back in the future

thanks
 
Last edited:
Back
Top Bottom