string(5) "68,78" [2] => string(5) "65,78" [3] => string(5) "60,82" 但是数据处理需要得到的内容应该为array(1){ {代码...} } 请..."/> string(5) "68,78" [2] => string(5) "65,78" [3] => string(5) "60,82" 但是数据处理需要得到的内容应该为array(1){ {代码...} } 请...">

首页  >  文章  >  后端开发  >  PHP 数组的拼接重组

PHP 数组的拼接重组

WBOY
WBOY原创
2016-08-23 09:17:463954浏览

现在的代码是这样的
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>

矩阵转置, 上面这个方法可以推广到多维矩阵

写一个仅为你这个情况作处理的,分析了一下,你这个情况是把数组内的字符串进行分割的处理

<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