转换为一个数组删除php中的重复值

发布于 2025-01-10 05:23:49 字数 3177 浏览 0 评论 0原文

每个数组都包含具有相同学生 ID 的不同类型的考试。 数组值推送第一个数组接收值。

数组1

Array ( 
    [0] => Array ( [student_id] => 1190101 [stname] => Student 1 [remarks] => [totals] => 21 ) 
    [1] => Array ( [student_id] => 1190102 [stname] => Student 2 [remarks] => 1 [totals] => 6 ) 
    [2] => Array ( [student_id] => 1190103 [stname] => Student 3 [remarks] => 
        [totals] => 11 ) 
    [3] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => 1 [totals] => 6 ) 
    [4] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => 1 [totals] => 6 ) 
    [5] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => 1 [totals] => 6 ) 
    [6] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => 1 [totals] => 6 ) 
    [7] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => 1 [totals] => 6 ) 
    [8] => Array ( [student_id] => 1190109 [stname] => Student 9 [remarks] => 1 [totals] => 6 ) 
    [9] => Array ( [student_id] => 1190110 [stname] => Student 10 [remarks] => 1 [totals] => 6 ) )

数组2

Array ( 
    [0] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => [totals] => 5 ) 
    [1] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => [totals] => 5 ) )

数组3

Array (
    [0] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => [totals] => 5 ) 
    [1] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => [totals] => 5 )
    [2] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => [totals] => 5 ) 
)

需要单个多维数组,例如

Array ( 
    [0] => Array ( [student_id] => 1190101 [stname] => Student 1 [remarks] => 1 [totals] => 21 ) 
    [1] => Array ( [student_id] => 1190102 [stname] => Student 2 [remarks] => 1 [totals] => 6 ) 
    [2] => Array ( [student_id] => 1190103 [stname] => Student 3 [remarks] => [totals] => 11 ) 
    [3] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => 1 [totals] => 6*[totals] => 5* ) 
    [4] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => 1 [totals] =>4 [totals] => 5 ) 
    [5] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => 1 [totals] => 6[totals] => 5 ) 
    [6] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => 1 [totals] => 6 [totals] => 5 ) 
    [7] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => 1 [totals] => 6 ) 
    [8] => Array ( [student_id] => 1190109 [stname] => Student 9 [remarks] => 1 [totals] => 6 [totals] => 5 ) 
    [9] => Array ( [student_id] => 1190110 [stname] => Student 10 [remarks] => 1 [totals] => 6 ) 
)

Each array hold different type of exam with same student Id.second
array value push first array receptive value .

Array 1

Array ( 
    [0] => Array ( [student_id] => 1190101 [stname] => Student 1 [remarks] => [totals] => 21 ) 
    [1] => Array ( [student_id] => 1190102 [stname] => Student 2 [remarks] => 1 [totals] => 6 ) 
    [2] => Array ( [student_id] => 1190103 [stname] => Student 3 [remarks] => 
        [totals] => 11 ) 
    [3] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => 1 [totals] => 6 ) 
    [4] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => 1 [totals] => 6 ) 
    [5] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => 1 [totals] => 6 ) 
    [6] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => 1 [totals] => 6 ) 
    [7] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => 1 [totals] => 6 ) 
    [8] => Array ( [student_id] => 1190109 [stname] => Student 9 [remarks] => 1 [totals] => 6 ) 
    [9] => Array ( [student_id] => 1190110 [stname] => Student 10 [remarks] => 1 [totals] => 6 ) )

array 2

Array ( 
    [0] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => [totals] => 5 ) 
    [1] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => [totals] => 5 ) )

array 3

Array (
    [0] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => [totals] => 5 ) 
    [1] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => [totals] => 5 )
    [2] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => [totals] => 5 ) 
)

Need Single multidimensional array Like

Array ( 
    [0] => Array ( [student_id] => 1190101 [stname] => Student 1 [remarks] => 1 [totals] => 21 ) 
    [1] => Array ( [student_id] => 1190102 [stname] => Student 2 [remarks] => 1 [totals] => 6 ) 
    [2] => Array ( [student_id] => 1190103 [stname] => Student 3 [remarks] => [totals] => 11 ) 
    [3] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => 1 [totals] => 6*[totals] => 5* ) 
    [4] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => 1 [totals] =>4 [totals] => 5 ) 
    [5] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => 1 [totals] => 6[totals] => 5 ) 
    [6] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => 1 [totals] => 6 [totals] => 5 ) 
    [7] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => 1 [totals] => 6 ) 
    [8] => Array ( [student_id] => 1190109 [stname] => Student 9 [remarks] => 1 [totals] => 6 [totals] => 5 ) 
    [9] => Array ( [student_id] => 1190110 [stname] => Student 10 [remarks] => 1 [totals] => 6 ) 
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云雾 2025-01-17 05:23:49

您可以迭代每个数组并将所有数据放入一个新数组中,使用学生 ID 作为键:

$new_array = [];
$old_arrays = [$array1, $array2, $array3];

foreach( $old_arrays as $old_array ) {
    foreach( $old_array as $key => $value ) {
        $new_array[ $old_array['student_id'] ][$key] = $value;
    }
}

因此每次它找到已添加的学生时,它都会再次添加信息。如果 array2 有关于 123 号学生的一条数据,而 array1 没有,则会通过 key 将其添加到新数组中的 123 号学生的数组中。

这假设每个学生 ID 都是正确的,并且每个 ID 中只有一个学生,每个学生只有一个 ID,并且其他信息(如 stname 或备注)在不同的数组中没有冲突。

You could iterate through each array and put all the data into a new array, using student ID as the key:

$new_array = [];
$old_arrays = [$array1, $array2, $array3];

foreach( $old_arrays as $old_array ) {
    foreach( $old_array as $key => $value ) {
        $new_array[ $old_array['student_id'] ][$key] = $value;
    }
}

So each time it finds a student that was already added, it will add the information again. If array2 has a piece of data on student #123 that array1 didn't have, it will be added to student #123's array in the new array by the key.

This assumes that each student ID is correct, and there is only one student per ID and one ID per student, and that none of the other info (like stname or remarks) are conflicting in different arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文