I'm new to php, only been writing it for 3 days now...
I'm creating a scheduling app for college students and one of the database tables contains 2 primary keys obtained from two different tables. (Student_ID
, Slot_ID
)
I have created 2 arrays. One array containing all of the students ID's and the other array containing all of the Slot ID's
Now I want to loop through all of the student id's and assign it a slot from the other array and then insert both of these values into a third table called Student_Slot
which holds (Student_ID
, Slot_ID
).
I've tried the Array_Combine
method like the below and using the array with the lowest elements.
$min = min(count($PartTimeStudents), count($EveningTimeSlots));
$result = array_combine(array_slice($PartTimeStudents, 0, $min), array_slice($EveningTimeSlots, 0, $min));
print_r($result);
This throws 2 errors:
Notice: Array to string conversion
If anyone knows a better way to construct this in PHP or a solution around what I'm trying to achieve that would be great.
Also: I know the errors are occurring because the Arrays are holding arrays inside rather than the String or INT data however i can't find a work around.
UPDATED:
The Array Structures are;
// Part Time Students
$sql = "SELECT Student_ID FROM student WHERE PartTime =1";
$result = mysqli_query($con, $sql);
$PartTimeStudents = array();
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$PartTimeStudents [] = $row;
}
}
// EveningTimeSlots
$sql = "SELECT * FROM slot WHERE StartTime >= '18:00:00'";
$result = mysqli_query($con, $sql);
$EveningTimeSlots = array();
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$EveningTimeSlots [] = $row;
}
}
The content of the arrays are
PartTimeStudents:
Array(
[0] => Array ( [Student_ID] => 1235 )
[1] => Array ( [Student_ID] => 65432789 )
[2] => Array ( [Student_ID] => 123456789 )
[3] => Array ( [Student_ID] => 987654321 )
)
EveningTimeSlot:
Array (
[0] => Array ( [Slot_ID] => 3 [Day] => Monday [Date] => 2018-05-28 [StartTime] => 19:00:00 [EndTime] => 20:00:00 [Room_ID] => 1 )
[1] => Array ( [Slot_ID] => 4 [Day] => Tuesday [Date] => 2018-05-28 [StartTime] => 20:00:00 [EndTime] => 21:00:00 [Room_ID] => 1 )
)
- There will be more slots, small data for testing purposes.
I want to pair [Student_ID] + [Slot_ID] and insert them into the table, It doesn't matter what slot gets assigned to what student as long as every student in the array gets a slot. Hope that clears it up.
Fixed
That worked perfectly! Exactly what i was trying to achieve! Thanks for the help.
My new code looks like:
//function to extract student id from students array
function extractStudentId($PartTimeStudents){
return $PartTimeStudents['Student_ID'];
} // Changed $array to $PartTimeStudents
//function to extract slot id from Slot array
function extractSlotId($EveningTimeSlots){
return $EveningTimeSlots['Slot_ID'];
} // changed $array to $EveningTimeSlots
$students = array_map("extractStudentId",$PartTimeStudents);//new students array
$slots =array_map("extractSlotId",$EveningTimeSlots);//new slots array
$min = min(count($students), count($slots));
$result = array_combine(array_slice($students, 0, $min), array_slice($slots, 0, $min));
foreach($result as $key=>$value){
$sql = ("INSERT INTO student_slot (Student_ID, Slot_ID)VALUES ('$key' , '$value')");
mysqli_query ($con, $sql);
}