PHP Возвращает объект и массив, когда объявление похоже - PullRequest
1 голос
/ 23 марта 2020

У меня проблема с возвратом моих многомерных массивов. По какой-то причине два из них возвращаются как объекты, а не как массивы, когда инициализация практически одинакова.

Я создаю временную карточку и хотел бы вернуть% времени, когда сотрудник опаздывал и приходил в месяц в общей сложности за последние 12 месяцев.

Код:

$eID = $_GET['showLatencyValues'];

//get the last 12 months
$month = time();
for ($i = 1; $i <= 12; $i++) {
  $month = strtotime( date( 'Y-m-01' )." -$i months");
  $months[] = date("r", $month);
}


$ii = 0;    
for($i = 11; $i >= 0; $i--) {
    $m = explode(" ", $months[$i]);
    $result = $m[2].'. '.$m[3];
    $last12months[$ii][0] = $ii;
    $last12months[$ii][1] = $result;
    $ii++;
}

//set the correct dates
$f = explode(" ", $last12months[0][1]);
$t = explode(" ", $last12months[11][1]);

$from   = $f[1].'-'.letterToNumeric($f[0]).'-01';
$days   = cal_days_in_month(CAL_GREGORIAN, letterToNumeric($t[0]), $t[1]); // 31
$to     = $t[1].'-'.letterToNumeric($t[0]).'-'.$days;

//get the information from database
$sql = mysqli_query($conn, "SELECT DISTINCT `timeIn`,`date` FROM `clock` WHERE `eID`='$eID' AND date BETWEEN '$from' AND '$to'");
$res = mysqli_fetch_array($sql);
$break = explode("-", $res['date']);
$month = $break[1]; //set current month to
$countMonth = 0; //month number
$count = 0; //day number
$sql = mysqli_query($conn, "SELECT min(timeIn) as timeIn,`date` FROM `clock` WHERE `eID`='$eID' AND date BETWEEN '$from' AND '$to' group by date");
while($res = mysqli_fetch_array($sql)) {
    $break = explode("-", $res['date']); //get the current selected month
    if($month != $break[1]) {           //check if current month is the same as selected by database if not then
        $countMonth++; //add current month number
        $count = 0;    //set current day number to 0
        $month = $break[1]; //set the current month to whatever is selected by database
    }
    $timeInData[$countMonth][$count] = $res['timeIn'];
    $count++;
}

$count = 0;
for($i = 0; $i < count($timeInData); $i++) {
    for($s = 0; $s < count($timeInData[$i]); $s++) {
        $firstShift_timeIn = setting($conn, 'timeIn1');
        $secondShift_timeIn = setting($conn, 'timeIn2');
        $lateConsideredAfter = setting($conn, 'lateAfter');

        $actualTime = date("H:i", strtotime($timeInData[$i][$s]));
        if($res_shift['shift'] == 1)    $considerLateTime = date("H:i", strtotime($firstShift_timeIn." +".$lateConsideredAfter." minutes"));
        else                            $considerLateTime = date("H:i", strtotime($secondShift_timeIn." +".$lateConsideredAfter." minutes"));

        if($actualTime > $considerLateTime) $late[$count]++;
        else $onTime[$count]++;                 
    }
    $count++;
}

if($count != 12) {
    $amt = (12-($count))-1;
    for($w = 0; $w <= $amt; $w++) {
        $onTimeYesData[$w][0] = $w;
        $onTimeYesData[$w][1] = 0;
        $onTimeNoData[$w][0] = $w;
        $onTimeNoData[$w][1] = 0;
    }
}

for($q = ($amt+1); $q <= 11; $q++) {
    $total = $onTime[($q-($amt+1))] + $late[($q-($amt+1))];

    $onTimeYesData[$q][0] = $q;

    $onTimeYesData[$q][1] = round(($onTime[($q-($amt+1))]/$total)*100);

    $onTimeNoData[$q][0] = $q;

    $onTimeNoData[$q][1] = round(($late[($q-($amt+1))]/$total)*100);
}

$response[1] = $last12months;
$response[0] = $onTimeYesData;
$response[2] = $onTimeNoData;

echo json_encode($response[1])."\n\n".json_encode($response[0])."\n\n".json_encode($response[2]);

Вывод, который я получаю:

[[0, "март 2019"], [1, "апр. 2019"], [2, "май. 2019" ], [3, «июнь 2019»], [4, «июль 2019»], [5, «август 2019»], [6, «сентябрь 2019»], [7, «октябрь 2019» ], [8, «ноябрь 2019»], [9, «De c. 2019»], [10, «январь 2020»], [11, «февраль 2020»]]]

{ "1": [1,90], "2": [2100], "3": [3,96], "4": [4,91], "5": [5,95], "6": [6,95], "7": [7100], "8": [8,92], "9": [9,95], "10": [10,89], "11 ": [11,88]}

{" 1 ": [1,10]," 2 ": [2,0]," 3 ": [3,4]," 4 ": [ 4,9], "5": [5,5], "6": [6,5], "7": [7,0], "8": [8,8], "9": [ 9,5], «10»: [10,11], «11»: [11,13]}

Я ожидаю, что два других выхода будут в том же формате, что и первый .

Любая помощь очень ценится. Благодаря.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...