简体   繁体   中英

How to convert array format

I have a array like this

   $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"];

I want to convert it like this

$row = array (
    array('Boxname', 'X1', 'X2', 'Y2'),
    array('LL', '53.3', '106.6'),

);

Can anybody tell how to do this. Am confused with this. Please help me!

Your question is a bit unclear, is this a csv? You can format it like this: 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);

This outputs:

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

If you want the data inside a csv file, then just use fputcsv() function:

$fp = fopen('your_csv_file.csv', 'w');
foreach($name as $fields) {
    $fields = explode(',', $fields);
    fputcsv($fp, $fields);
}

Note: you must have write permissions.

I think this code will do:

$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>";
}

OUTPUT:

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
)

Try this code. This works.

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

This is the Output:

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"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM