doutu6658
2018-05-03 14:32
浏览 33
已采纳

如何将两个不同数组中的两个ID插入到数据库表中?

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);
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsbpaqt61965 2018-05-03 21:03
    已采纳

    you have to extract each id from its array to create simple array then you can use array array_combine you can use array_map function like this

    first create function to be applied for each record in array

    //function to extract student id from students rray
    function extractStudentId($array){
    return $array['Student_ID'];
    }
    //function to extract slot id from Slot array
    function extractSlotId($array){
    return $array['Slot_ID'];
    }
    

    then use array_map to create new array

    $students = array_map("extractStudentId",$PartTimeStudents);//new students array
    $slots =array_map("extractSlotId",$EveningTimeSlots);//new slots array 
    

    then you can use array_combine to create new array containing student_id and slot_id like you have done but with the new arrays

    $min = min(count($students), count($slots));
    $result = array_combine(array_slice($students, 0, $min), array_slice($slots, 0, $min));
    

    you can use foreach loop to insert new data like this

    foreach($result as $key=>$value){
    }
    

    lastly have a look at array_map documentation for better understanding http://php.net/manual/en/function.array-map.php

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题