У меня есть массив из импорта CSV, который я хочу объединить на основе идентификатора.
Структура выглядит следующим образом:
Каждая строка является мерой и добавлением комментария к представлению. Для одного представления может быть несколько строк. Например, если есть 3 меры, данные представления заголовка будут повторяться 3 раза, с различными комментариями / деталями измерения.
//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 )
Части 0 – 13 каждого массива одинаковы, это основная информация для каждого представления. Я хочу попытаться добавить 14 – 17 части массива в новый массив. Ниже приведен пример:
[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 ) ) ) )
Я полностью в тупике, но понятия не имею, с чего начать. Могу ли я создать новый массив и нажать на него данные в зависимости от идентификатора отправки ( [0] => 1
массива)?
Это должно привести к преобразованию данных, которое вы хотите. Небольшое дополнение к вашему предыдущему вопросу.
<?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);
Что производит
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 ) ) ) ) )
Я думаю, что что-то вроде этого должно это сделать:
//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 : Я не тестировал этот код.
Disclaimar 2 : он не дает вам точный формат, который вы хотите, но комментарии должны дать вам представление о том, как настроить его в соответствии с вашими точными потребностями.
вы можете закончить код в 2-3 строках. используйте array_splice
для формирования нового массива по вашему выбору. http://php.net/manual/en/function.array-splice.php
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);