I've got a huge list of arrays with 2 members which looks like that:
<?php
$aData = array ();
$aData[0] = array ( 14400, 'Item1' );
$aData[1] = array ( 14400, 'Item4' );
$aData[2] = array ( 14401, 'Item2' );
$aData[3] = array ( 14402, 'Item4' );
$aData[4] = array ( 14402, 'Item5' );
$aData[5] = array ( 14402, 'Item9' );
and I'd like to convert this into arrays like that:
$aData[0] = array ( 14400, 'Item1,Item4' );
$aData[1] = array ( 14401, 'Item4' );
$aData[2] = array ( 14402, 'Item4,Item5,Item9' );
Whereas the 2nd one could be a string or an array. Now I was trying to implement some logic - but is there a function that could do that at least partly? array_map
or array_merge_recursive
don't seem to fit exactly?
Use a Map of (int -> string list)
for each (key,value) pair
map.get(key).add(value)
What language are you using?
Because you don't know what you you want. You can easy receive such result'
$arr = [
[14400,Item1],
[14400,Item4],
[14401,Item2],
[14402,Item4],
[14402,Item5],
[14402,Item9]
];
$new = [];
foreach($arr as $item)
$new[$item[0]][] = $item[1];
print_r($new);
result
Array
(
[14400] => Array
(
[0] => Item1
[1] => Item4
)
[14401] => Array
(
[0] => Item2
)
[14402] => Array
(
[0] => Item4
[1] => Item5
[2] => Item9
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.