我正在使用PHP并拥有一个类似于以下内容的数组:

时间:2014-04-07 11:34:53

标签: php arrays multidimensional-array foreach array-merge

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
        )

)

3 个答案:

答案 0 :(得分:0)

// 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

答案 1 :(得分:0)

好的,这是逻辑,我还没有测试过它。

将结果保存到一个新数组中,如$ 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);
}

答案 2 :(得分:0)

好的

试试这种方式:)

$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);

我认为我得到了正确答案,但最好检查一下:)