I have a string of numbers separated by : like "20:1:21,21:0:21", this php code explodes the strings with , and and concatenate the resulting arrays into an associative array like:
$in = "20:1:21,21:0:21";
$list=explode(",",$in);
$results = array( 0 => array(), 1 => array());
foreach ($list as $item) {
$item=explode(":",$item);
if (sizeof($item)==3) {
$results[$item[1]][$item[0]] += $item[2];
}
}
note the += operator here.
The expected value of
array(2) {
[0]=>
array(1) {
[21]=>
int(21)
}
[1]=>
array(1) {
[20]=>
int(21)
}
}
or [{"21":21},{"20":21}] as json output.
In javascript it could be like
var results = { 0: [], 1: [] };
for (var key in list) {
var item = list[key];
item=item.split(":");
if (item.length == 3) {
if(!results[item[1]]) results[item[1]]={};
results[item[1]][item[0]]=item[2];
}
}
but it creates a list of null values before appending the right values, why?
$invariable contains.list). Which is it? Please provide samples of the input (in JS format), the expected output for it and what you get.+=with=it will produce the results you want.