PHP - 将数组的不同部分组合在一起以添加到数据库中

时间:2015-10-23 12:35:12

标签: php arrays associative-array

我有一个CSV导入的数组,我想根据ID将它们组合在一起。

结构如下:

每一行都是一项度量,并在提交中添加了评论。一次提交可以有多行。例如,如果有3个度量标准,则标题提交数据将重复3次,并带有不同的评论/度量详细信息。

        [0] => Array
            (
                [0] => 1
                [1] => Lot
                [2] => Lot Submission
                [3] => Lot Submission
                [4] => Lot Submission
                [5] => LNW North
                [6] => C Spencer Ltd
                [7] => Panel
                [8] => 1
                [9] => Buildings
                [10] => 2015/2016
                [11] => 2
                [12] => 1,2,3,4,5,6,7,8
                [13] => Testing notes
                [14] => KPI1 - Behavioural Safety
                [15] => PER1 - Health, Safety, and Wellbeing strategy
                [16] => Testing Comment 1
                [17] => Expected
            )

        [1] => Array
            (
                [0] => 1
                [1] => Lot
                [2] => Lot Submission
                [3] => Lot Submission
                [4] => Lot Submission
                [5] => LNW North
                [6] => C Spencer Ltd
                [7] => Panel
                [8] => 1
                [9] => Buildings
                [10] => 2015/2016
                [11] => 2
                [12] => 1,2,3,4,5,6,7,8
                [13] => Testing notes
                [14] => KPI1 - Behavioural Safety
                [15] => PMTEST - Test
                [16] => Testing 2
                [17] => Stretch
            )

        [2] => Array
            (
                [0] => 1
                [1] => Lot
                [2] => Lot Submission
                [3] => Lot Submission
                [4] => Lot Submission
                [5] => LNW North
                [6] => C Spencer Ltd
                [7] => Panel
                [8] => 1
                [9] => Buildings
                [10] => 2015/2016
                [11] => 2
                [12] => 1,2,3,4,5,6,7,8
                [13] => Testing notes
                [14] => KPI1 - Behavioural Safety
                [15] => JP001 - Jamie
                [16] => Testing 3
                [17] => Excellence
            )

Parts 0-13 of each array are the same, this is the headline information for each submission.我想尝试将数组的第14-17部分添加到新数组中。一个例子如下

[0] => Array
    (
        [0] => 1
        [1] => Lot
        [2] => Lot Submission
        [3] => Lot Submission
        [4] => Lot Submission
        [5] => LNW North
        [6] => C Spencer Ltd
        [7] => Panel
        [8] => 1
        [9] => Buildings
        [10] => 2015/2016
        [11] => 2
        [12] => 1,2,3,4,5,6,7,8
        [13] => Testing notes
        ['measure'] = array(
            [0] = array(
                [14] => KPI1 - Behavioural Safety
                [15] => PER1 - Health, Safety, and Wellbeing strategy
                ['comments'] = array(
                    [16] => Testing Comment 1
                    [17] => Expected
                )
            ),
            [1] = array(
                [14] => KPI1 - Behavioural Safety
                [15] => PMTEST - Test
                ['comments'] = array(
                    [16] => Testing 2
                    [17] => Stretch
                )
            ),
            [2] = array(
                [14] => KPI1 - Behavioural Safety
                [15] => JP001 - Jamie
                ['comments'] = array(
                    [16] => Testing 3
                    [17] => Excellence
                )
            )
        )                   
    )

我完全难过,但不知道从哪里开始。我可以创建一个新数组并根据提交ID(数组的[0] => 1)将数据推送到它吗?

任何帮助或指导都将是如此大规模的帮助

1 个答案:

答案 0 :(得分:1)

此代码会将数组转换为您想要的格式的新数组。

唯一的区别是它不会保留measurecomments数组中的原始出现次数。我不明白为什么他们需要这样做,但如果你认为他们应该这样,我可以相应地更改代码。

$new = array();

foreach ( $old as $occ => $val ) {

    // move over the first 14 from first row
    if ( $occ == 0 ) {
       for( $i=0; $i<14; $i++ ) {
            $new[] = $old[$occ][$i];
        }
        // setup the new measure array
        $new['measure'] = array();
    }

    $t = array();
    for ( $i=14; $i < 16; $i++ ) {
        $t[] = $old[$occ][$i];
    }
    $new['measure'][] = $t;

    $t = array();
    for ( $i=16; $i < count($old[$occ]); $i++ ) {
        $t[] = $old[$occ][$i];
    }
    $new['measure'][$occ]['comments'] = $t;
}
print_r($new);

结果是: -

Array
(
    [0] => 1
    [1] => Lot
    [2] => Lot Submission
    [3] => Lot Submission
    [4] => Lot Submission
    [5] => LNW North
    [6] => C Spencer Ltd
    [7] => Panel
    [8] => 1
    [9] => Buildings
    [10] => 2015/2016
    [11] => 2
    [12] => 1,2,3,4,5,6,7,8
    [13] => Testing notes
    [measure] => Array
        (
            [0] => Array
                (
                    [0] => KPI1 - Behavioural Safety
                    [1] => PER1 - Health, Safety, and Wellbeing strategy
                    [comments] => Array
                        (
                            [0] => Testing Comment 1
                            [1] => Expected
                        )

                )

            [1] => Array
                (
                    [0] => KPI1 - Behavioural Safety
                    [1] => PMTEST - Test
                    [comments] => Array
                        (
                            [0] => Testing 2
                            [1] => Stretch
                        )

                )

            [2] => Array
                (
                    [0] => KPI1 - Behavioural Safety
                    [1] => JP001 - Jamie
                    [comments] => Array
                        (
                            [0] => Testing 3
                            [1] => Excellence
                        )

                )

        )

)