Pooshonk
Pooshonk

Reputation: 1324

Group together different parts of array from CSV using PHP?

I have an array from a CSV import that I want to group together based on an ID.

The structure is as follows:

Each row is a measure and a comment added to a submission. There can be multiple rows for one submission. For example, if there are 3 measures, then the headline submission data will be repeated 3 times, with different comments/measure details.

//this is the submission id
[1] => Array
        [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
            )
[2] => Array
        [0] => Array
            (
                [0] => 2
                [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] => 2
                [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
            )

Parts 0 - 13 of each array are the same, this is the headline information for each submission. I want to try and add the 14th - 17th parts of the array to a new array. An example is below:

[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
        ['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
                )
            )
        )                   
    )

[2] => Array
    (
        [0] => 2
        [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
                )
            )
        )                   
    )

I am totally stumped, but have no idea where to start. Can I create a new array and push the data to it depending on the submission ID ([0] => 1 of the array)?

Upvotes: 0

Views: 46

Answers (4)

Amarnasan
Amarnasan

Reputation: 15529

function transform($array) {
    $newArray = [];
    foreach($array as $item) {
        for($ii = 0; $ii <= 13; $ii++) {
            $newArray[$ii] = $item[$ii];
        }
        if (!isset($newArray['measure'])) {
            $newArray['measure'] = [];
        }
        $newArray['measure'][] = [
            14 => $item[14],
            15 => $item[15],
            'comments' => [
                16 => $item[16],
                17 => $item[17],
            ]
        ];
    }

    return $newArray;
}

$submission = [];   /** <== $submission is your array */

$newSubmission = [] ;
foreach($submission as $array) {
    $newSubmission [] = transform($array);
}

print_r($newSubmission);

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94662

This should produce the data conversion you want it. A small addition to your previous question.

<?php
$original =
array(
       array(
             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'
                ),
            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'
                ),
            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'
                )
            ),
        array(
            array(
                0 => '2',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'
                ),
            array(
                0 => '2',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'
                )
        )
);

$new = array();

foreach ( $original as $idx => $old ) {

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

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

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

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

print_r($new);

Which produces

Array
(
    [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
                        (
                            [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
                                )
                        )
                )
        )

    [1] => Array
        (
            [0] => 2
            [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
                                )
                        )
                )
        )
)

Upvotes: 1

Manish Shukla
Manish Shukla

Reputation: 71

you can finish code in 2-3 lines. use array_splice to form new array of your choice. http://php.net/manual/en/function.array-splice.php

Upvotes: 0

Anders
Anders

Reputation: 8577

I think something like this should do it:

//A new array to hold the result.
$result = array();

//Loop through the array.
foreach($array as $item) {
    //Get the ID of the current item (that is the first element, I assume).
    $id = $item[0];
    //If the there is no entry for this ID in result...
    if(!isset($result[$id])) {
        //...then add it:
        $result[$id] = array();
        //Add entry 0-13:
        for($i=0; $i<=13; $i++) {
            $result[$id][$i] = $item[$i];
        }
        //Add an entry 14 to hold the other stuff.
        $result[$id][14] = array();
    }
    //Now add entry 14-17 from the input to the array in entry 14 of the result.
    $result[$id][14][] = array(
        $item[14],
        $item[15],
        $item[16],
        $item[17],
    );
}

Disclaimar: I have not tested this code.

Disclaimar 2: It does not give you the exact format you want, but the comments should give you an idea of how to tune it to suit your exact needs.

Upvotes: 1

Related Questions