Array
(
[1] => Array
(
[1] => Title
[2] => Exam1
)
[2] => Array
(
[1] => class_name
[2] => date
[3] => start_time
[4] => finish_time
[5] => subject
[6] => exam_details
)
[3] => Array
(
[1] => 10
[2] => 2014-03-08
[3] => 2014-03-26 08:44:15
[4] => 2014-03-26 08:44:15
[5] => Maths
[6] => Extra comments
)
[4] => Array
(
[1] => 10
[2] => 2014-03-07
[3] => 2014-03-26 08:44:15
[4] => 2014-03-26 08:44:15
[5] => Eng
[6] => Extra comments
)
[5] => Array
(
[1] => 10
[2] => 2014-03-06
[3] => 2014-03-26 08:44:15
[4] => 2014-03-26 08:44:15
[5] => Science
[6] => Extra comments
)
[6] => Array
(
[1] => Title
[2] => Exam2
)
[7] => Array
(
[1] => class_name
[2] => date
[3] => start_time
[4] => finish_time
[5] => subject
[6] => exam_details
)
[8] => Array
(
[1] => 10
[2] => 2014-03-08
[3] => 2014-03-26 08:44:15
[4] => 2014-03-26 08:44:15
[5] => Maths
[6] => Extra comments
)
[9] => Array
(
[1] => 10
[2] => 2014-03-07
[3] => 2014-03-26 08:44:15
[4] => 2014-03-26 08:44:15
[5] => Eng
[6] => Extra comments
)
[10] => Array
(
[1] => 10
[2] => 2014-03-06
[3] => 2014-03-26 08:44:15
[4] => 2014-03-26 08:44:15
[5] => Science
[6] => Extra comments
)
)
如您所见,第一个数组和第二个数组中有一些标题和标题名称定义字段名称,其他三个是该字段的值,我想使用考试标题划分数组,像这样
[Exam1] =>Array
(
[2] => Array
(
[class_name] => 10
[date] => 2014-03-08
[start_time] => 2014-03-26 08:44:15
[finish_time] => 2014-03-26 08:44:15
[subject] => Maths
[exam_details] => Extra comments
)
[3] => Array
(
[class_name] => 10
[date] => 2014-03-07
[start_time] => 2014-03-26 08:44:15
[finish_time] => 2014-03-26 08:44:15
[subject] => Eng
[exam_details] => Extra comments
)
[4] => Array
(
[class_name] => 10
[date] => 2014-03-06
[start_time] => 2014-03-26 08:44:15
[finish_time] => 2014-03-26 08:44:15
[subject] => Science
[exam_details] => Extra comments
)
)
[Exam2] =>Array
(
[7] => Array
(
[class_name] => 10
[date] => 2014-03-08
[start_time] => 2014-03-26 08:44:15
[finish_time] => 2014-03-26 08:44:15
[subject] => Maths
[exam_details] => Extra comments
)
[8] => Array
(
[class_name] => 10
[date] => 2014-03-07
[start_time] => 2014-03-26 08:44:15
[finish_time] => 2014-03-26 08:44:15
[subject] => Eng
[exam_details] => Extra comments
)
[9] => Array
(
[class_name] => 10
[date] => 2014-03-06
[start_time] => 2014-03-26 08:44:15
[finish_time] => 2014-03-26 08:44:15
[subject] => Science
[exam_details] => Extra comments
)
)
好的,这是逻辑,我还没有测试过。
将结果保存到新数组中,例如$array。
// Generate result set
$i=0;
foreach($array as $arr) {
if(count($arr)==2) {
$i++;
$newarray[$i] = array();
} else {
foreach($arr as $single){
array_push($newarray[$i], $single);
}
}
}
// New result array
foreach($newarray as $a_new) {
print_r($a_new);
}
Ok
试试这种方式:)
$badArray = array();
$goodArray = array();
$i = FALSE;
$k = FALSE;
foreach($badArray as $key=>$value){
if( !empty($value[1] && $value[1] === 'Title')){
$i = TRUE;
$k = $value[2];
$goodArray[$value[2]] = [];
continue;
}
if( $i == TRUE){
$i = FALSE;
$keysArray = $value;
continue;
}
$goodArray[$k][] = array_combine(array_values($keysArray), array_values($value));
}
echo "<pre>";
var_dump($goodArray);
我想我得到了正确的答案,但最好检查一下:)
// split array into small chunks
$arrChunked = array_chunk($myArray, 5);
$formattedArray = array();
// loop through array chunk
foreach ($arrChunked as $arr)
{
// get titles
$title = current($arr);
$title = $title[2];
// get key from this title
$key = key($arr);
// remove title from array
unset($arr[$key]);
// get the array without titles and append to new formatted array
$formattedArray[$title] = $arr;
}
参考array_chunk