我有一个这样的数组:
$lessonOptions=array();
$lessonOptions[0]=array("Physics","6.00","2015-01-01","-","4");
$lessonOptions[1]=array("Physics","16.00","2015-01-01","-","2");
$lessonOptions[2]=array("Maths","10.00","2015-07-01","-","10");
$lessonOptions[3]=array("Maths","20.00","2015-07-01","-","10");
我想用一个名为optionarray的新数组创建一个输出,其中包含: **
physics 6.00 2015-01-04 -
physics 16.00 2015-01-01 -
数学10.00 2015-07-01 -
数学20.00 2015-07-07 -
我的问题是我可以看到每一行数学但只缺少一行物理。 如何显示所有行?
我的实际代码是:
$lessonGroup=$lessonOptions[0][0];
$display=FALSE;
$state=0;
$nbLessons=count($lessonsOptions);
for ($i = 0; $i<$nbLessons; $i++)
{
if ($i+1!=$nbLessons) $lessonGroupSuivant=$lessonOptions[$i+1][0];
else $display=TRUE;
switch ($state)
{
case 0 :
$infoLesson[$state]=$lessonOptions[$i];
default :
{
if ($lessonGroup==$lessonGroupSuivant)
{
$infoLesson[$state]=$lessonOptions[$i];
$state=$state+1;
}
else
{
$display=TRUE;
$lessonGroup=$lessonGroupSuivant;
$state=0;
}
}
}
if ($display==TRUE)
{
//var_dump($infoLesson);
$display=FALSE;
}
}
我的实际数组(错误) 正如你所看到的,我的数组的大小是1,我需要2,因为我有两个课程物理。
array(1) { [0]=> array(5) { [0]=> string(31) "physics" [1]=> string(10) "6.00" [2]=> string(10) "2015-01-01" [3]=> string(1) "-" [4]=> float(42) } }
array(2) { [0]=> array(5) { [0]=> string(15) "Maths" [1]=> string(11) "10.00" [2]=> string(10) "2015-07-01" [3]=> string(1) "-" [4]=> float(10) } [1]=> array(5) { [0]=> string(15) "Maths" [1]=> string(11) "10.00" [2]=> string(10) "2015-07-12" [3]=> string(1) "-" [4]=> float(10) } }
答案 0 :(得分:0)
好吧,我想我找到了你需要的东西:
<?php
$lessonOptions=array();
$lessonOptions[0]=array("Physics","6.00","2015-01-01","-","4");
$lessonOptions[1]=array("Physics","16.00","2015-01-01","-","2");
$lessonOptions[2]=array("Maths","10.00","2015-07-01","-","10");
$lessonOptions[3]=array("Maths","20.00","2015-07-01","-","10");
$optionarray = array();
$lastLessonKey = '';
$i = 0;
foreach ($lessonOptions as $key => $data) {
if (empty($lastLessonKey)) {
$lastLessonKey = $data[0];
} else if ($lastLessonKey !== $data[0]) {
$i++;
$lastLessonKey = $data[0];
}
$optionarray[$i][] = $data;
}
var_dump($optionarray);
输出:
array(2) { [0]=> array(2) { [0]=> array(5) { [0]=> string(7) "Physics" [1]=> string(4) "6.00" [2]=> string(10) "2015-01-01" [3]=> string(1) "-" [4]=> string(1) "4" } [1]=> array(5) { [0]=> string(7) "Physics" [1]=> string(5) "16.00" [2]=> string(10) "2015-01-01" [3]=> string(1) "-" [4]=> string(1) "2" } } [1]=> array(2) { [0]=> array(5) { [0]=> string(5) "Maths" [1]=> string(5) "10.00" [2]=> string(10) "2015-07-01" [3]=> string(1) "-" [4]=> string(2) "10" } [1]=> array(5) { [0]=> string(5) "Maths" [1]=> string(5) "20.00" [2]=> string(10) "2015-07-01" [3]=> string(1) "-" [4]=> string(2) "10" } } }
答案 1 :(得分:0)
因此,您愿意按某个值对数组进行分组
$infoLesson = array();
foreach ($lessonOptions as $v) {
$infoLesson[$v[0]][] = $v;
}
然后,您可以按键检索数组。 例如打印 $ infoLesson [&#39;数学&#39;] 将为您提供
Array
(
[0] => Array
(
[0] => Maths
[1] => 10.00
[2] => 2015-07-01
[3] => -
[4] => 10
)
[1] => Array
(
[0] => Maths
[1] => 20.00
[2] => 2015-07-01
[3] => -
[4] => 10
)
)
$ infoLesson [&#39; Physics&#39;]
Array
(
[0] => Array
(
[0] => Physics
[1] => 6.00
[2] => 2015-01-01
[3] => -
[4] => 4
)
[1] => Array
(
[0] => Physics
[1] => 16.00
[2] => 2015-01-01
[3] => -
[4] => 2
)
)