как зациклить посещаемость сотрудника с помощью array_search, чтобы получить текущее количество сотрудников посещаемости из объекта данных

Вот мои данные

{
"data": [
{
"firstname": "Amol",
"lastname": "Auti",
"emp_primary": "1",
"emp_id": "1001",
"company_name": "Maverick Solution",
"id": "1",
"in_time": "09:00:00",
"out_time": "16:00:00",
"total_time": "07:00:00.000000",
"date": "2018-08-04",
"device_id": "12:12:12:12"},
{
"firstname": "Amol",
"lastname": "Auti",
"emp_primary": "1",
"emp_id": "1001",
"company_name": "Maverick Solution",
"id": "18",
"in_time": "09:00:00",
"out_time": "16:00:00",
"total_time": "07:00:00.000000",
"date": "2018-08-05",
"device_id": "12:12:12:12"},
{
"firstname": "Amol",
"lastname": "Auti",
"emp_primary": "1",
"emp_id": "1001",
"company_name": "Maverick Solution",
"id": "5",
"in_time": "09:00:00",
"out_time": "12:00:00",
"total_time": "03:00:00.000000",
"date": "2018-08-06",
"device_id": "12:12:12:12"},
{
"firstname": "Sumit",
"lastname": "Malusare",
"emp_primary": "2",
"emp_id": "1002",
"company_name": "Maverick Solution",
"id": "20",
"in_time": "09:00:00",
"out_time": "18:00:00",
"total_time": "09:00:00.000000",
"date": "2018-08-05",
"device_id": "12:12:12:12"}
]
}

где emp_primary = 1 имеет 3 записи, а emp_primary 2 имеет 1 запись

И вот моя актуальная проблема

public function getdaterange($start, $end, $format = 'Y-m-d') {
$start  = new DateTime($start);
$end    = new DateTime($end);
$invert = $start > $end;
$dates = array();
$dates[] = $start->format($format);
while ($start != $end) {
$start->modify(($invert ? '-' : '+') . '1 day');
$dates[] = $start->format($format);
}
return $dates;
}

public function export(){
$getdateRangeVal = $this->getdaterange($from, $to);
foreach ($_data as $value) {
$arrstatus[$value->emp_primary] = $value;
for($i = 0; $i<count($getdateRangeVal); $i++){
$key = array_search($getdateRangeVal[$i], array_column($_data, 'date'));
if(strlen($key) > 0) {
$arrstatus[$value->emp_primary]->dateRange[] = array(
"status" => "P",
"getdateRangeVal" => $getdateRangeVal[$i],
"intime" => $_data[$key]->in_time,
"outtime" => $_data[$key]->out_time,
"totaltime" => $_data[$key]->total_time,
);`enter code here`
} else {
$arrstatus[$value->emp_primary]->dateRange[] = array(
"status"=>"A",
"getdateRangeVal"=>$getdateRangeVal[$i],
"intime" => "",
"outtime" => "",
"totaltime" => "",
);
}
}
}
}

и это результат этой программы

Array
(
[1] => stdClass Object
(
[firstname] => Amol
[lastname] => Auti
[emp_primary] => 1
[emp_id] => 1001
[company_name] => Maverick Solution
[id] => 5
[in_time] => 09:00:00
[out_time] => 12:00:00
[total_time] => 03:00:00.000000
[date] => 2018-08-06
[device_id] => 12:12:12:12
[dateRange] => Array
(
[0] => Array
(
[status] => A
[getdateRangeVal] => 2018-08-01
[intime] =>
[outtime] =>
[totaltime] =>
)
[1] => Array
(
[status] => A
[getdateRangeVal] => 2018-08-02
[intime] =>
[outtime] =>
[totaltime] =>
)
[2] => Array
(
[status] => A
[getdateRangeVal] => 2018-08-03
[intime] =>
[outtime] =>
[totaltime] =>
)
[3] => Array
(
[status] => P
[getdateRangeVal] => 2018-08-04
[intime] => 09:00:00
[outtime] => 16:00:00
[totaltime] => 07:00:00.000000
)
[4] => Array
(
[status] => P
[getdateRangeVal] => 2018-08-05
[intime] => 09:00:00
[outtime] => 16:00:00
[totaltime] => 07:00:00.000000
)
[5] => Array
(
[status] => P
[getdateRangeVal] => 2018-08-06
[intime] => 09:00:00
[outtime] => 12:00:00
[totaltime] => 03:00:00.000000
)
)
)

[2] => stdClass Object
(
[firstname] => Sumit
[lastname] => Malusare
[emp_primary] => 2
[emp_id] => 1002
[company_name] => Maverick Solution
[id] => 20
[in_time] => 09:00:00
[out_time] => 18:00:00
[total_time] => 09:00:00.000000
[date] => 2018-08-05
[device_id] => 12:12:12:12
[dateRange] => Array
(
[0] => Array
(
[status] => A
[getdateRangeVal] => 2018-08-01
[intime] =>
[outtime] =>
[totaltime] =>
)
[1] => Array
(
[status] => A
[getdateRangeVal] => 2018-08-02
[intime] =>
[outtime] =>
[totaltime] =>
)
[2] => Array
(
[status] => A
[getdateRangeVal] => 2018-08-03
[intime] =>
[outtime] =>
[totaltime] =>
)
[3] => Array
(
[status] => P
[getdateRangeVal] => 2018-08-04
[intime] => 09:00:00
[outtime] => 16:00:00
[totaltime] => 07:00:00.000000
)
[4] => Array
(
[status] => P
[getdateRangeVal] => 2018-08-05
[intime] => 09:00:00
[outtime] => 16:00:00
[totaltime] => 07:00:00.000000
)
[5] => Array
(
[status] => P
[getdateRangeVal] => 2018-08-06
[intime] => 09:00:00
[outtime] => 12:00:00
[totaltime] => 03:00:00.000000
)
)
)
)

посмотрите на данные, я должен добавить 3 записи в emp_primary 1 или массив [1] и 1 запись во второй массив, но первая запись emp назначается другой записи all
Помогите мне, пожалуйста.

1

Решение

изменил свою функцию.

public function export(){
$getdateRangeVal = $this->getdaterange($from, $to);
foreach ($_data as $value) {
$valToStore = $value;
for($i = 0; $i<count($getdateRangeVal); $i++){
$key = array_search($getdateRangeVal[$i], array_column($_data, 'date'));
if(strlen($key) > 0) {
$valToStore->dateRange[] = array(
"status" => "P",
"getdateRangeVal" => $getdateRangeVal[$i],
"intime" => $_data[$key]->in_time,
"outtime" => $_data[$key]->out_time,
"totaltime" => $_data[$key]->total_time,
);
} else {
$valToStore->dateRange[] = array(
"status"=>"A",
"getdateRangeVal"=>$getdateRangeVal[$i],
"intime" => "",
"outtime" => "",
"totaltime" => "",
);
}
}
$arrstatus[$value->emp_primary][] = $valToStore;
}
}

попробуй один раз.

0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]