laravel 比较两个数组中的值(即时间),然后将它们合并到一个新的基于时间的排序数组中

juud5qan  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(109)

我有2个数组。第一个数组包含全天的时间表,其中包含上午8点到晚上11:59之间的60分钟的时隙。

0 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "07:00:00"
    "to" => "08:00:00"
    "status" => "available"
    "type" => ""
  ]
  1 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "08:00:00"
    "to" => "09:00:00"
    "status" => "available"
    "type" => ""
  ]
  2 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "09:00:00"
    "to" => "10:00:00"
    "status" => "available"
    "type" => ""
  ]
  3 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "10:00:00"
    "to" => "11:00:00"
    "status" => "available"
    "type" => ""
  ]
  4 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "11:00:00"
    "to" => "12:00:00"
    "status" => "available"
    "type" => ""
  ]
  5 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "12:00:00"
    "to" => "13:00:00"
    "status" => "available"
    "type" => ""
  ]
  6 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "13:00:00"
    "to" => "14:00:00"
    "status" => "available"
    "type" => ""
  ]

我有第二个数组,它包含了时间表或已预订的时间

^ array:8 [▼
  0 => array:6 [▼
    "student_name" => "Davin Daugherty"
    "date" => "2022-11-11"
    "from" => "10:00:00"
    "to" => "10:30:00"
    "status" => "booked"
    "type" => "theory"
  ]
  1 => array:5 [▼
    "student_name" => ""
    "date" => "2022-11-11"
    "from" => "10:30:00"
    "to" => "11:00:00"
    "status" => "not available"
  ]
  2 => array:6 [▼
    "student_name" => "Davin Daugherty"
    "date" => "2022-11-11"
    "from" => "11:00:00"
    "to" => "12:00:00"
    "status" => "booked"
    "type" => "theory"
  ]
  3 => array:6 [▼
    "student_name" => "Davin Daugherty"
    "date" => "2022-11-11"
    "from" => "12:00:00"
    "to" => "13:00:00"
    "status" => "booked"
    "type" => "theory"
  ]
  4 => array:5 [▼
    "student_name" => ""
    "date" => "2022-11-11"
    "from" => "13:00:00"
    "to" => "14:00:00"
    "status" => "available"
  ]
  5 => array:5 [▼
    "student_name" => ""
    "date" => "2022-11-11"
    "from" => "14:00:00"
    "to" => "15:00:00"
    "status" => "absent"
  ]
  6 => array:6 [▼
    "student_name" => "Davin Daugherty"
    "date" => "2022-11-11"
    "from" => "15:00:00"
    "to" => "16:00:00"
    "status" => "booked"
    "type" => "theory"
  ]

因此,我想比较第一个数组中的时间“from”和第二个数组中的“from”。如果类在10和11之间预订,那么我想创建一个新数组,该数组包含预订数组中的对象,如果没有预订类,那么我想从默认时间表数组中获取对象。
因此在结尾i将具有包含来自两个数组的数据的数组。
最后一个数组应该是这样

0 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "07:00:00"
    "to" => "08:00:00"
    "status" => "available"
    "type" => ""
  ]
  1 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "08:00:00"
    "to" => "09:00:00"
    "status" => "available"
    "type" => ""
  ]
  2 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "09:00:00"
    "to" => "10:00:00"
    "status" => "available"
    "type" => ""
  ]
  3 => array:6 [▼
    "student_name" => "Davin Daugherty"
    "date" => "2022-11-11"
    "from" => "10:00:00"
    "to" => "10:30:00"
    "status" => "booked"
    "type" => "theory
  ]
  4 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-11"
    "from" => "10:30:00"
    "to" => "11:00:00"
    "status" => "not available"
  ]
  5 => array:6 [▼
    "student_name" => "Davin Daugherty"
    "date" => "2022-11-11"
    "from" => "11:00:00"
    "to" => "12:00:00"
    "status" => "booked"
    "type" => "theory"
  ]
  5 => array:6 [▼
    "student_name" => ""
    "date" => "2022-11-06"
    "from" => "12:00:00"
    "to" => "13:00:00"
    "status" => "available"
    "type" => ""
  ]
  6 => array:6 [▼
    "student_name" => "Alex"
    "date" => "2022-11-11"
    "from" => "13:00:00"
    "to" => "14:00:00"
    "status" => "Booked"
    "type" => "practical"
  ]
wwwo4jvm

wwwo4jvm1#

你可以这样做

$scheduledClasses = collect($scheduledArray);
$bookedClasses = collect($bookeArray);
$newArray = [];
foreach($scheduledClasses as $secheduledClass){
if($bookedClass->where('from', $scheduledClass->from)->count() >0){
$newArray[] = $bookedClass->where('from', $scheduledClass->from)->first();
}else{
$newArray[] = $scheduledClass;
}

相关问题