一个巨大的数据库值重现问题我手动简化并在这里给出
在此代码中
$array = array(
"0" => array("course_id" => 1,"register_number"=>"16BCA01"),
"1" => array("course_id" => 1,"register_number"=>"16BCA02"),
"2" => array("course_id" => 1,"register_number"=>"16BCA03"),
"3" => array("course_id" => 1,"register_number"=>"16BCA04"),
"4" => array("course_id" => 2,"register_number"=>"16BCA05"),
"5" => array("course_id" => 2,"register_number"=>"16BCA06"),
"6" => array("course_id" => 3,"register_number"=>"16BCA07"),
"7" => array("course_id" => 3,"register_number"=>"16BCA08"),
"8" => array("course_id" => 4,"register_number"=>"16BCA09"),
"9" => array("course_id" => 4,"register_number"=>"16BCA10"),
"10" => array("course_id" => 5,"register_number"=>"16BCA11"),
"11" => array("course_id" => 5,"register_number"=>"16BCA12"),
"12" => array("course_id" => 6,"register_number"=>"16BCA13"),
"13" => array("course_id" => 6,"register_number"=>"16BCA14"),
"14" => array("course_id" => 6,"register_number"=>"16BCA15"),
"15" => array("course_id" => 7,"register_number"=>"16BCA16"),
"16" => array("course_id" => 7,"register_number"=>"16BCA17"),
"17" => array("course_id" => 7,"register_number"=>"16BCA18"),
"18" => array("course_id" => 8,"register_number"=>"16BCA19"),
"19" => array("course_id" => 8,"register_number"=>"16BCA20"),
"20" => array("course_id" => 9,"register_number"=>"16BCA21")
) ;
print_r($array);
答案将像顺序中的course_id一样
Array (
[0] => Array ( [course_id] => 1 [register_number] => 16BCA01 )
[1] => Array ( [course_id] => 1 [register_number] => 16BCA02 )
[2] => Array ( [course_id] => 1 [register_number] => 16BCA03 )
[3] => Array ( [course_id] => 1 [register_number] => 16BCA04 )
[4] => Array ( [course_id] => 2 [register_number] => 16BCA05 )
[5] => Array ( [course_id] => 2 [register_number] => 16BCA06 )
[6] => Array ( [course_id] => 3 [register_number] => 16BCA07 )
[7] => Array ( [course_id] => 3 [register_number] => 16BCA08 )
[8] => Array ( [course_id] => 4 [register_number] => 16BCA09 )
[9] => Array ( [course_id] => 4 [register_number] => 16BCA10 )
[10] => Array ( [course_id] => 5 [register_number] => 16BCA11 )
[11] => Array ( [course_id] => 5 [register_number] => 16BCA12 )
[12] => Array ( [course_id] => 6 [register_number] => 16BCA13 )
[13] => Array ( [course_id] => 6 [register_number] => 16BCA14 )
[14] => Array ( [course_id] => 6 [register_number] => 16BCA15 )
[15] => Array ( [course_id] => 7 [register_number] => 16BCA16 )
[16] => Array ( [course_id] => 7 [register_number] => 16BCA17 )
[17] => Array ( [course_id] => 7 [register_number] => 16BCA18 )
[18] => Array ( [course_id] => 8 [register_number] => 16BCA19 )
[19] => Array ( [course_id] => 8 [register_number] => 16BCA20 )
[20] => Array ( [course_id] => 9 [register_number] => 16BCA21 ) )
但我希望course_id有像这样的替代值
Array (
[0] => Array ( [course_id] => 1 [register_number] => 16BCA01 )
[1] => Array ( [course_id] => 2 [register_number] => 16BCA05 )
[2] => Array ( [course_id] => 1 [register_number] => 16BCA02 )
[3] => Array ( [course_id] => 2 [register_number] => 16BCA06 )
[4] => Array ( [course_id] => 1 [register_number] => 16BCA03 )
[5] => Array ( [course_id] => 3 [register_number] => 16BCA07 )
[6] => Array ( [course_id] => 1 [register_number] => 16BCA04 )
[7] => Array ( [course_id] => 3 [register_number] => 16BCA08 )
[8] => Array ( [course_id] => 4 [register_number] => 16BCA09 )
[9] => Array ( [course_id] => 5 [register_number] => 16BCA11 )
[10] => Array ( [course_id] => 4 [register_number] => 16BCA10 )
[11] => Array ( [course_id] => 5 [register_number] => 16BCA12 )
[12] => Array ( [course_id] => 6 [register_number] => 16BCA13 )
[13] => Array ( [course_id] => 7 [register_number] => 16BCA16 )
[14] => Array ( [course_id] => 6 [register_number] => 16BCA14 )
[15] => Array ( [course_id] => 7 [register_number] => 16BCA17 )
[16] => Array ( [course_id] => 6 [register_number] => 16BCA15 )
[17] => Array ( [course_id] => 7 [register_number] => 16BCA18 )
[18] => Array ( [course_id] => 8 [register_number] => 16BCA19 )
[19] => Array ( [course_id] => 9 [register_number] => 16BCA21 )
[20] => Array ( [course_id] => 8 [register_number] => 16BCA20 ) )
course_id 1和2应该首先如果任一个id完成意味着它应该移动到下一个id 3或4同样直到它应该循环结束。这意味着course_id不应该重复与之前的值相同的值如何在php
中实现这一点答案 0 :(得分:1)
您需要创建自己的逻辑。使用嵌套的for循环和数组函数,您可以实现您的要求。请参阅以下代码,它会对您有所帮助。
$array = array(
"0" => array("course_id" => 1,"register_number"=>"16BCA01"),
"1" => array("course_id" => 1,"register_number"=>"16BCA02"),
"2" => array("course_id" => 1,"register_number"=>"16BCA03"),
"3" => array("course_id" => 1,"register_number"=>"16BCA04"),
"4" => array("course_id" => 2,"register_number"=>"16BCA05"),
"5" => array("course_id" => 2,"register_number"=>"16BCA06"),
"6" => array("course_id" => 3,"register_number"=>"16BCA07"),
"7" => array("course_id" => 3,"register_number"=>"16BCA08"),
"8" => array("course_id" => 4,"register_number"=>"16BCA09"),
"9" => array("course_id" => 4,"register_number"=>"16BCA10"),
"10" => array("course_id" => 5,"register_number"=>"16BCA11"),
"11" => array("course_id" => 5,"register_number"=>"16BCA12"),
"12" => array("course_id" => 6,"register_number"=>"16BCA13"),
"13" => array("course_id" => 6,"register_number"=>"16BCA14"),
"14" => array("course_id" => 6,"register_number"=>"16BCA15"),
"15" => array("course_id" => 7,"register_number"=>"16BCA16"),
"16" => array("course_id" => 7,"register_number"=>"16BCA17"),
"17" => array("course_id" => 7,"register_number"=>"16BCA18"),
"18" => array("course_id" => 8,"register_number"=>"16BCA19"),
"19" => array("course_id" => 8,"register_number"=>"16BCA20"),
"20" => array("course_id" => 9,"register_number"=>"16BCA21")
);
$new_array = array();
$k = 1;
for($i = 0; $i<=count($array);$i++)
{
$i = 0;
$new_array[]= $array[$i];
for($j=0;$j<=count($array);$j++)
{
if($array[$i]['course_id'] != $array[$j]['course_id'])
{
$new_array[]= $array[$j];
unset($array[$i]);
unset($array[$j]);
$array = array_values($array);
break;
}
}
}
array_pop($new_array);
print_r($new_array);