PHP array concatenation and reorganization

WBOY
Release: 2016-08-23 09:17:46
Original
3954 people have browsed it

The current code is like this
array(6) {
[0] => string(5) "58,86"
[1] => string(5) "68,78"
[2] = > string(5) "65,78"
[3] => string(5) "60,82"

But the content required for data processing should be
array(1){

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

}

Please tell me how to process the data

After thinking about it, the idea is to traverse first, then split the values ​​and then splice them together. However, during the final assembly, there is an empty key. I don’t know where it comes from.
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>
Copy after login
Copy after login
After

we get
array(7) {
[0] => array(2) {

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

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

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

}
[1] => array(2) {

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

}
[2] => array(2) {

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

}
[3] => array(2) {

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

}
[4] => array(1) {

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

}
[5] => array(1) {

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

}
}
Then assemble the same key into a new array. Because my PHP version is 5.4 and I cannot use array_column, I wrote a public method i_array_column()

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

Get 1 array
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>
Copy after login
Copy after login

}
["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>
Copy after login
Copy after login

}
}
Because highcharts requires json format, convert it to json format
dump(json_encode($b));,
The result after accurate conversion is
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}}"

I just don’t know why there is an empty array

Reply content:

The current code is like this
array(6) {
[0] => string(5) "58,86"
[1] => string(5) "68,78"
[2] = > string(5) "65,78"
[3] => string(5) "60,82"

But the content required for data processing should be
array(1){

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

}

Please tell me how to process the data

After thinking about it, the idea is to traverse first, then split the values ​​and then splice them together. However, during the final assembly, there is an empty key. I don’t know where it comes from.
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>
Copy after login
Copy after login
After

we get
array(7) {
[0] => array(2) {

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

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

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

}
[1] => array(2) {

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

}
[2] => array(2) {

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

}
[3] => array(2) {

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

}
[4] => array(1) {

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

}
[5] => array(1) {

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

}
}
Then assemble the same key into a new array. Because my PHP version is 5.4 and I cannot use array_column, I wrote a public method i_array_column()

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

Get 1 array
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>
Copy after login
Copy after login

}
["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>
Copy after login
Copy after login

}
}
Because highcharts requires json format, convert it to json format
dump(json_encode($b));,
The result after accurate conversion is
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}}"

I just don’t know why there is an empty array

<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>
Copy after login

Matrix transpose, the above method can be extended to multi-dimensional matrices

Write one that only handles your situation. After analysis, your situation is to split the strings in the array

<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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template