string(5) "68,78" [2] => string(5) "65,78" [3] => string(5) "60,">
时间:2021-07-01 10:21:17 帮助过:36人阅读
但是数据处理需要得到的内容应该为
array(1){
[0] => 58,68,65,60
[1] => 86,78,78,82
}
请问下 数据处理 如何处理
想了想之后 思路是 先遍历然后 拆分值 再拼接 只不过在最后组装的时候有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){
$a[''] = $a[$key] = explode(',',$val);
}
之后得到
array(7) {
[0] => array(2) {
[0] => string(2) "58"
[1] => string(2) "86"
}
[""] => array(1) {
[0] => string(2) "40"
}
[1] => array(2) {
[0] => string(2) "68"
[1] => string(2) "78"
}
[2] => array(2) {
[0] => string(2) "65"
[1] => string(2) "78"
}
[3] => array(2) {
[0] => string(2) "60"
[1] => string(2) "82"
}
[4] => array(1) {
[0] => string(3) "094"
}
[5] => array(1) {
[0] => string(2) "40"
}
}
然后在把相同的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) {
[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"
}
["data2"] => array(7) {
[0] => string(2) "86"
[""] => NULL
[1] => string(2) "78"
[2] => string(2) "78"
[3] => string(2) "82"
[4] => NULL
[5] => NULL
}
}
因为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){
[0] => 58,68,65,60
[1] => 86,78,78,82
}
请问下 数据处理 如何处理
想了想之后 思路是 先遍历然后 拆分值 再拼接 只不过在最后组装的时候有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){
$a[''] = $a[$key] = explode(',',$val);
}
之后得到
array(7) {
[0] => array(2) {
[0] => string(2) "58"
[1] => string(2) "86"
}
[""] => array(1) {
[0] => string(2) "40"
}
[1] => array(2) {
[0] => string(2) "68"
[1] => string(2) "78"
}
[2] => array(2) {
[0] => string(2) "65"
[1] => string(2) "78"
}
[3] => array(2) {
[0] => string(2) "60"
[1] => string(2) "82"
}
[4] => array(1) {
[0] => string(3) "094"
}
[5] => array(1) {
[0] => string(2) "40"
}
}
然后在把相同的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) {
[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"
}
["data2"] => array(7) {
[0] => string(2) "86"
[""] => NULL
[1] => string(2) "78"
[2] => string(2) "78"
[3] => string(2) "82"
[4] => NULL
[5] => NULL
}
}
因为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}}"
只不过不知道为什么会有一个空的数组
矩阵转置, 上面这个方法可以推广到多维矩阵
写一个仅为你这个情况作处理的,分析了一下,你这个情况是把数组内的字符串进行分割的处理
$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);