我有一个CSV导入的数组,我想根据ID将它们组合在一起。
结构如下:
每一行都是一项度量,并在提交中添加了评论。一次提交可以有多行。例如,如果有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
)
)
)
)
我完全难过,但不知道从哪里开始。我可以创建一个新数组并根据提交ID(数组的[0] => 1
)将数据推送到它吗?
答案 0 :(得分:1)
我觉得这样的事情应该这样做:
//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 :它没有为您提供您想要的确切格式,但评论应该让您了解如何调整它以满足您的确切需求。
答案 1 :(得分: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
)
)
)
)
)
答案 2 :(得分:0)
你可以用2-3行完成代码。使用array_splice
形成您选择的新数组。
http://php.net/manual/en/function.array-splice.php
答案 3 :(得分:0)
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);