0

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.

        [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. I want to try and add the 14th - 17th parts of the array to a new array. An example is below

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

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)?

Any help or guidance will be such a massive help

0

1 Answer 1

1

This code will convert the array into a new array of the format you said you wanted.

The only difference being it does not keep the original occurances counts in the measure and comments arrays. I could not see why they needed to be that same, but if you think they should be I can change the code accordingly.

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

And the result is :-

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
                        )

                )

        )

)
Sign up to request clarification or add additional context in comments.

7 Comments

Was this any use to you?
Just managed to get back to my computer. No this doesn't working unfortunately. However this is very close. I will have a play around and update this question
In what way does it not work? It produces the output you wanted, except for the fact that the indexes in measure and comment start at 0. If you actually want them to use the same indexes as in your question, thats easily done. Let me know!
Since then I have had to update my code, i will update my question
No it works fine, but my structure has changed. See my updated question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.