首頁  >  文章  >  後端開發  >  PHP 陣列的拼接重組

PHP 陣列的拼接重組

WBOY
WBOY原創
2016-08-23 09:17:463945瀏覽

現在的程式碼是這樣的
array(6) {
[0] => string(5) "58,86"
[1] => string(5) "68,78"
[2] => string (5) "65,78"
[3] => string(5) "60,82"

但是資料處理需要得到的內容應該是
array(1){

<code>[0] => 58,68,65,60
[1] => 86,78,78,82</code>

}

請問下 資料處理 如何處理

想了想之後思路是先遍歷然後拆分值再拼接只不過在最後組裝的時候有1個空的key 不知道從哪裡來的
array(6) {
[0] => string(5) "58,86"
[1] => string(5) "68,78"
[2] => string(5) "65,78"
[3] => string(5) "60,82"
[4] => string(3) "094"
[5] => string(2) "40"
}、

foreach($lsj['data'] as $key=>$val){

<code>    $a[''] = $a[$key] = explode(',',$val);
   }
   </code>

之後得到
array(7) {
[0] => array(2) {

<code>[0] => string(2) "58"
[1] => string(2) "86"</code>

}
[""] => array(1) {

<code>[0] => string(2) "40"</code>

}
[1] => array(2) {

<code>[0] => string(2) "68"
[1] => string(2) "78"</code>

}
[2] => array(2) {

<code>[0] => string(2) "65"
[1] => string(2) "78"</code>

}
[3] => array(2) {

<code>[0] => string(2) "60"
[1] => string(2) "82"</code>

}
[4] => array(1) {

<code>[0] => string(3) "094"</code>

}
[5] => array(1) {

<code>[0] => string(2) "40"</code>

}
}
然後在把相同的key 組裝成一個新的數組 因為本人的PHP版本為5.4無法使用array_column 所有寫了一個公共的方法 i_array_column()

$b['data1'] = i_array_column($a,'0');
$b['data2'] = i_array_column($a,'1');

得到1個陣列
array(2) {
["data1"] => array(7) {

<code>[0] => string(2) "58"
[""] => string(2) "40"
[1] => string(2) "68"
[2] => string(2) "65"
[3] => string(2) "60"
[4] => string(3) "094"
[5] => string(2) "40"</code>

}
["data2"] => array(7) {

<code>[0] => string(2) "86"
[""] => NULL
[1] => string(2) "78"
[2] => string(2) "78"
[3] => string(2) "82"
[4] => NULL
[5] => NULL</code>

}
}
因為highcharts需要json格式所以轉換json格式
dump(json_encode($b));、
準換後結果為
string(146) "{"data1":{"0":"58" ,"":"40","1":"68","2":"65","3":"60","4":"094","5":"40"}," data2":{"0":"86","":null,"1":"78","2":"78","3":"82","4":null,"5" :null}}"

只不過不知道為什麼會有一個空的陣列

回覆內容:

現在的程式碼是這樣的
array(6) {
[0] => string(5) "58,86"
[1] => string(5) "68,78"
[2] => string (5) "65,78"
[3] => string(5) "60,82"

但是資料處理需要得到的內容應該是
array(1){

<code>[0] => 58,68,65,60
[1] => 86,78,78,82</code>

}

請問下 資料處理 如何處理

想了想之後思路是先遍歷然後拆分值再拼接只不過在最後組裝的時候有1個空的key 不知道從哪裡來的
array(6) {
[0] => string(5) "58,86"
[1] => string(5) "68,78"
[2] => string(5) "65,78"
[3] => string(5) "60,82"
[4] => string(3) "094"
[5] => string(2) "40"
}、

foreach($lsj['data'] as $key=>$val){

<code>    $a[''] = $a[$key] = explode(',',$val);
   }
   </code>

之後得到
array(7) {
[0] => array(2) {

<code>[0] => string(2) "58"
[1] => string(2) "86"</code>

}
[""] => array(1) {

<code>[0] => string(2) "40"</code>

}
[1] => array(2) {

<code>[0] => string(2) "68"
[1] => string(2) "78"</code>

}
[2] => array(2) {

<code>[0] => string(2) "65"
[1] => string(2) "78"</code>

}
[3] => array(2) {

<code>[0] => string(2) "60"
[1] => string(2) "82"</code>

}
[4] => array(1) {

<code>[0] => string(3) "094"</code>

}
[5] => array(1) {

<code>[0] => string(2) "40"</code>

}
}
然後在把相同的key 組裝成一個新的數組 因為本人的PHP版本為5.4無法使用array_column 所有寫了一個公共的方法 i_array_column()

$b['data1'] = i_array_column($a,'0');
$b['data2'] = i_array_column($a,'1');

得到1個陣列
array(2) {
["data1"] => array(7) {

<code>[0] => string(2) "58"
[""] => string(2) "40"
[1] => string(2) "68"
[2] => string(2) "65"
[3] => string(2) "60"
[4] => string(3) "094"
[5] => string(2) "40"</code>

}
["data2"] => array(7) {

<code>[0] => string(2) "86"
[""] => NULL
[1] => string(2) "78"
[2] => string(2) "78"
[3] => string(2) "82"
[4] => NULL
[5] => NULL</code>

}
}
因為highcharts需要json格式所以轉換json格式
dump(json_encode($b));、
準換後結果為
string(146) "{"data1":{"0":"58" ,"":"40","1":"68","2":"65","3":"60","4":"094","5":"40"}," data2":{"0":"86","":null,"1":"78","2":"78","3":"82","4":null,"5" :null}}"

只不過不知道為什麼會有一個空的陣列

<code class="php"><?php 
$arr = ["58,86,1,18",
"68,78,2,17",
"65,78,3,17",
"60,82,4,19",
"4,5,6,18"];


array_walk($arr, function(&$item) {
    $item = explode(',', $item);
});

$new_arr = [];
$length = count($arr);
for ($i = 0; $i < $length; $i++) {
    $length1 = count($arr[$i]);
    for ($j = 0; $j < $length1; $j++) {
        $new_arr[$j][$i] = $arr[$i][$j];
    }
}

array_walk($new_arr, function(&$item) {
    $item = join(',', $item);
});

print_r($new_arr);</code>

矩陣轉置, 上面這個方法可以推廣到多維矩陣

寫一個僅為你這個情況作處理的,分析了一下,你這個情況是把數組內的字串進行分割的處理

<code>    $arr = array("58,86","68,78","65,78","60,82");
    foreach($arr as $key => $value){
        $tmp = explode(',',$value);
        $a[] = $tmp[0];
        $b[] = $tmp[1];
    }
    $c = array(
        implode(',',$a),implode(',',$b)
    );
    print_r($c);</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn