我试图重命名所有数组键从第一个维度在这个形式
Array
(
[0] => Array
(
[0] => Array
(
[0] => firstname17
[1] => lastname17
)
[1] => Array
(
[0] => firstname7
[1] => lastname7
)
[2] => Array
(
[0] => firstname18
[1] => lastname18
)
[3] => Array
(
[0] => firstname12
[1] => lastname12
)
[4] => Array
(
[0] => firstname3
[1] => lastname3
)
)
[1] => Array
(
[5] => Array
(
[0] => firstname13
[1] => lastname13
)
[6] => Array
(
[0] => firstname11
[1] => lastname11
)
[7] => Array
(
[0] => firstname19
[1] => lastname19
)
[8] => Array
(
[0] => firstname9
[1] => lastname9
)
[9] => Array
(
[0] => firstname8
[1] => lastname8
)
)
[2] => Array
(
[10] => Array
(
[0] => firstname10
[1] => lastname10
)
[11] => Array
(
[0] => firstname2
[1] => lastname2
)
[12] => Array
(
[0] => firstname4
[1] => lastname4
)
[13] => Array
(
[0] => firstname16
[1] => lastname16
)
[14] => Array
(
[0] => firstname15
[1] => lastname15
)
)
[3] => Array
(
[15] => Array
(
[0] => firstname1
[1] => lastname1
)
[16] => Array
(
[0] => firstname6
[1] => lastname6
)
[17] => Array
(
[0] => firstname20
[1] => lastname20
)
[18] => Array
(
[0] => firstname14
[1] => lastname14
)
[19] => Array
(
[0] => firstname5
[1] => lastname5
)
)
)
使用下面的PHP代码。当我运行它时,我在所有新的数组键中得到未定义的索引,我试图从块数组中保留键。"location1","location2","location3","location4","location5","location6","location"
<?php
$centers = array("location1","location2","location3","location4","location5","location6","location");
$row = '';
$filename = 'arrays.csv';
$handle = fopen($filename, "r"); //Open file for reading
if($handle){
while (($data = fgetcsv($handle, ",")) !== FALSE){
$state[] = $data;
shuffle($state);
}
$newarray = array_chunk($state, 5, true);
foreach($newarray as $key){
foreach($centers as $newkey){
$newarray[$newkey] = $centers[$newkey];
}
}
echo "<pre>";
print_r($newarray);
}
?>
编辑:预期的输出应该是这样的
Array
(
[location1] => Array
(
[0] => Array
(
[0] => firstname17
[1] => lastname17
)
[1] => Array
(
[0] => firstname7
[1] => lastname7
)
[2] => Array
(
[0] => firstname18
[1] => lastname18
)
[3] => Array
(
[0] => firstname12
[1] => lastname12
)
[4] => Array
(
[0] => firstname3
[1] => lastname3
)
)
[location2] => Array
(
[5] => Array
(
[0] => firstname13
[1] => lastname13
)
[6] => Array
(
[0] => firstname11
[1] => lastname11
)
[7] => Array
(
[0] => firstname19
[1] => lastname19
)
[8] => Array
(
[0] => firstname9
[1] => lastname9
)
[9] => Array
(
[0] => firstname8
[1] => lastname8
)
)
[location3] => Array
(
[10] => Array
(
[0] => firstname10
[1] => lastname10
)
[11] => Array
(
[0] => firstname2
[1] => lastname2
)
[12] => Array
(
[0] => firstname4
[1] => lastname4
)
[13] => Array
(
[0] => firstname16
[1] => lastname16
)
[14] => Array
(
[0] => firstname15
[1] => lastname15
)
)
[location4] => Array
(
[15] => Array
(
[0] => firstname1
[1] => lastname1
)
[16] => Array
(
[0] => firstname6
[1] => lastname6
)
[17] => Array
(
[0] => firstname20
[1] => lastname20
)
[18] => Array
(
[0] => firstname14
[1] => lastname14
)
[19] => Array
(
[0] => firstname5
[1] => lastname5
)
)
)
为了给它们提供新的密钥,您可以创建一个新的密钥,并将$centers
分配给原始的块数组。考虑这个例子:(我想这是来自你之前的另一个帖子)
$centers = array("location1","location2","location3","location4","location5","location6","location7");
$chunked_values = array( array( array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), ), array( array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), ), array( array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), ), array( array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), ), array( array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), ), array( array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), ), array( array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), array( 'firstname13', 'lastname13', 'Armstrong Landscaping', 'state1', 'email13', 'address13', ), ),);
$new_values = array();
foreach($chunked_values as $key => $value) {
$new_values[array_shift($centers)] = $value;
}
echo '<pre>';
print_r($new_values);
echo '</pre>';