我有一个像这样的数组
$name=["Boxname,X1,X2,Y1,Y2,PID,GENDER,AGE,Bp_Systolic,Bp_Diastolic,X-Value,Y-Value", "LL, 53.3,106.6,33.300000000000004,59.947215189873425,22,MALE,63,Bp_Systolic,120,Bp_Diastolic,80"];
我想像这样转换它
$row = array (
array('Boxname', 'X1', 'X2', 'Y2'),
array('LL', '53.3', '106.6'),
);
任何人都可以告诉你如何做到这一点。我很困惑。请帮帮我!
答案 0 :(得分:2)
你的问题有点不清楚,这是一个csv吗?您可以将其格式化为:Sample Output
$name=["Boxname,X1,X2,Y1,Y2,PID,GENDER,AGE,Bp_Systolic,Bp_Diastolic,X-Value,Y-Value", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:22, GENDER:MALE, Age:63,Bp_Systolic:120,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:27, GENDER:FEMALE, Age:56,Bp_Systolic:110,Bp_Diastolic:70"];
// get the headers (keys)
$headers = explode(',',array_shift($name));
$headers = array_splice($headers, 0, 10);
$data = array();
foreach($name as $info) {
// explode by comma, then explode the headers inside the values
$temp = array_map(function($piece){
return explode(':', $piece)[1];
}, explode(',', $info));
$data[] = array_combine($headers, $temp); // combine the headers (keys) to the values
}
echo '<pre>';
print_r($data);
输出:
Array
(
[0] => Array
(
[Boxname] => HH
[X1] => 53.3
[X2] => 106.6
[Y1] => 33.300000000000004
[Y2] => 59.947215189873425
[PID] => 22
[GENDER] => MALE
[AGE] => 63
[Bp_Systolic] => 120
[Bp_Diastolic] => 80
)
[1] => Array
(
[Boxname] => HH
[X1] => 53.3
[X2] => 106.6
[Y1] => 33.300000000000004
[Y2] => 59.947215189873425
[PID] => 27
[GENDER] => FEMALE
[AGE] => 56
[Bp_Systolic] => 110
[Bp_Diastolic] => 70
)
)
如果您想要csv文件中的数据,那么只需使用fputcsv()
函数:
$fp = fopen('your_csv_file.csv', 'w');
foreach($name as $fields) {
$fields = explode(',', $fields);
fputcsv($fp, $fields);
}
注意:您必须具有写入权限。
答案 1 :(得分:1)
我认为这段代码会:
$name= array("Boxname,X1,X2,Y1,Y2,PID,GENDER,AGE,Bp_Systolic,Bp_Diastolic,X-Value,Y-Value", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:22, GENDER:MALE, Age:63,Bp_Systolic:120,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:27, GENDER:FEMALE, Age:56,Bp_Systolic:110,Bp_Diastolic:70");
foreach($name as $data){
echo "<pre>";
print_r(explode(",",$data));
echo "</pre>";
}
输出:
Array
(
[0] => Boxname
[1] => X1
[2] => X2
[3] => Y1
[4] => Y2
[5] => PID
[6] => GENDER
[7] => AGE
[8] => Bp_Systolic
[9] => Bp_Diastolic
[10] => X-Value
[11] => Y-Value
)
Array
(
[0] => Boxname:HH
[1] => X1:53.3
[2] => X2:106.6
[3] => Y1:33.300000000000004
[4] => Y2:59.947215189873425
[5] => PID:22
[6] => GENDER:MALE
[7] => Age:63
[8] => Bp_Systolic:120
[9] => Bp_Diastolic:80
)
Array
(
[0] => Boxname:HH
[1] => X1:53.3
[2] => X2:106.6
[3] => Y1:33.300000000000004
[4] => Y2:59.947215189873425
[5] => PID:27
[6] => GENDER:FEMALE
[7] => Age:56
[8] => Bp_Systolic:110
[9] => Bp_Diastolic:70
)
答案 2 :(得分:1)
试试这段代码。这很有效。
$var = '["Boxname,X1,X2,Y1,Y2,PID,GENDER,AGE,Bp_Systolic,Bp_Diastolic,X-Value,Y-Value", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:22, GENDER:MALE, Age:63,Bp_Systolic:120,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:27, GENDER:FEMALE, Age:56,Bp_Systolic:110,Bp_Diastolic:70"]';
$result = array();
$main_array = json_decode($var, true);
for($i = 0; $i < count($main_array); $i++){
$temp_val = $main_array[$i];
$curr_array = explode(",", $temp_val);
$curr_temp_array = array();
for($j = 0; $j < count($curr_array); $j++){
$curr_item = explode(":", $curr_array[$j]);
if(count($curr_item) == 1){
$curr_temp_array[$j] = $curr_array[$j];
}else{
$curr_temp_array[$curr_item[0]] = $curr_item[1];
}
}
$result[$i] = $curr_temp_array;
}
var_dump($result);
这是输出:
array(3) {
[0]=>
array(12) {
[0]=>
string(7) "Boxname"
[1]=>
string(2) "X1"
[2]=>
string(2) "X2"
[3]=>
string(2) "Y1"
[4]=>
string(2) "Y2"
[5]=>
string(3) "PID"
[6]=>
string(6) "GENDER"
[7]=>
string(3) "AGE"
[8]=>
string(11) "Bp_Systolic"
[9]=>
string(12) "Bp_Diastolic"
[10]=>
string(7) "X-Value"
[11]=>
string(7) "Y-Value"
}
[1]=>
array(10) {
["Boxname"]=>
string(2) "HH"
[" X1"]=>
string(4) "53.3"
[" X2"]=>
string(5) "106.6"
[" Y1"]=>
string(18) "33.300000000000004"
["Y2"]=>
string(18) "59.947215189873425"
[" PID"]=>
string(2) "22"
[" GENDER"]=>
string(4) "MALE"
[" Age"]=>
string(2) "63"
["Bp_Systolic"]=>
string(3) "120"
["Bp_Diastolic"]=>
string(2) "80"
}
[2]=>
array(10) {
["Boxname"]=>
string(2) "HH"
[" X1"]=>
string(4) "53.3"
[" X2"]=>
string(5) "106.6"
[" Y1"]=>
string(18) "33.300000000000004"
["Y2"]=>
string(18) "59.947215189873425"
[" PID"]=>
string(2) "27"
[" GENDER"]=>
string(6) "FEMALE"
[" Age"]=>
string(2) "56"
["Bp_Systolic"]=>
string(3) "110"
["Bp_Diastolic"]=>
string(2) "70"
}
}