Merge All Sub Arrays into One

Merge all sub arrays into one

If it's only two levels of array, you can use

$result = call_user_func_array('array_merge', $array);

which should work as long as $array isn't completely empty

Transform Javascript subArrays into one Array

Edit: Please look at Dave's answer or p.s.w.g's answers instead - they're superior. This code is horrible and should not be used (I can't delete an accepted answer).

You could do something like this:

var arr = ['bla', ['ble', 'bli'], 'blo', ['blu']];
var newArr = arr.toString().split(',');

Demo

Explanation:

It's pretty simple. toString() on an array containing arrays creates a string with all the elements in the top level, so that [['abc']].toString() results in 'abc' regardless of how "deep" in the array it is. After that, you just split the string you get into an array using String.prototype.split and you've accomplished your goal

Python/Numpy joining subarrays of an array together

np.concatenate(array)

The only problem, in your example, array in the list array (now, do you see how bad it is to use confusing names) should become np.array and you should add commas after each numpy.ndarray in the list array:

array = [np.array([100000., 100848.06652142,  99648.67144694, 102047.67944271,
103655.99048427, 104602.87678005, 104597.83419837, 103505.42736768,
104976.01311214, 104457.34603404, 105855.46549162, 105613.30235519,
105212.71840922, 107647.5054673 ]),
np.array([107647.5054673 , 106891.82007643, 106979.91405552, 106030.74186486,
107856.04281712, 108502.71948581, 106557.2401056 , 105659.59829843,
105663.01875745, 107300.69453018, 106336.34733019, 107584.99034227,
108089.2320392 , 106786.91702337]),
np.array([106786.91702337, 104416.74000465, 101289.12525402, 101932.58219813,
102625.04352578, 101767.46209616, 103345.4263012 , 102816.73322055,
102417.59316407, 104439.37518975, 103755.22627215, 103817.9744686 ,
107872.40234863, 108110.9662065 ]),
np.array([108110.9662065 , 109544.86827069, 111072.22392645, 112618.46235766,
113847.1768898 , 116708.86391903, 115790.02599715, 115614.72242411,
119225.88436354, 121991.38468934, 123304.85972848, 125571.38040251,
122456.3667249 ]),
np.array([122456.3667249 , 127497.74699282, 128659.85495604, 125813.77115906,
129008.46450085, 128111.00914756, 123039.92607546, 124723.87932757,
124181.57385836, 125134.9276196 , 126027.8631434 , 129304.85119148,
128912.58600657])]

Merge/flatten an array of arrays



ES2019

ES2019 introduced the Array.prototype.flat() method which you could use to flatten the arrays. It is compatible with most environments, although it is only available in Node.js starting with version 11, and not at all in Internet Explorer.

const arrays = [
["$6"],
["$12"],
["$25"],
["$25"],
["$18"],
["$22"],
["$10"]
];
const merge3 = arrays.flat(1); //The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
console.log(merge3);

php combine two array's subarrays where value of certain key is equal

Try this:

foreach($arr_b as $b_item) {
foreach($arr_a as $key => &$a_item) {
if ($b_item['usrsID'] == $a_item['usrsID']) {
$a_item['id'] = $b_item['usrsID'];
$a_item['avatarID'] = $b_item['avatarID'];
}
}
}

Your output of $_arr_a will be:

Array
(
[0] => Array
(
[ID] => 5
[usrsID] => 3
[tid] => 19
[txtid] => 22
[id] => 3
[avatarID] => 22
)

[1] => Array
(
[ID] => 6
[usrsID] => 1
[tid] => 19
[txtid] => 23
[id] => 1
[avatarID] => 1
)

[2] => Array
(
[ID] => 7
[usrsID] => 2
[tid] => 19
[txtid] => 24
[id] => 2
[avatarID] => 3
)

[3] => Array
(
[ID] => 8
[usrsID] => 1
[tid] => 19
[txtid] => 25
[id] => 1
[avatarID] => 1
)

)

Merging all sub-arrays with mutual elements into one sub-array

Note that there is no "first key" -- dicts don't keep order, so if you need some order preserved you'll need to start from some different, alternative data structure.

Apart from order-related issues, I'd start with something like:

def merged(dictoflists):
result = dict()
reversed = dict()
for k, l in dictoflists.iteritems():
intersecting = set(reversed.get(w) for w in l) - set([None])
if intersecting:
pickone = intersecting.pop()
into = result[pickone]
else:
pickone = k
into = result[k] = set()
for ok in intersecting:
into.update(result.pop(ok))
into.update(l)
for w in into:
reversed[w] = pickone
return dict((k, sorted(l)) for k, l in result.iteritems())

If order is important to you, the uses of set will be problematic and you'll need more complicated (and slower) data structures -- however, if that's the case, you should first specify in complete detail exactly what ordering constraints you need to respect in the various possible cases that can occur.

How to merge sub-sub-arrays and add their lengths by the sub-array index?

You can use reduce to summarize the array. Use forEach to loop thru the inner array.

let arrayA = [[["a"],["b","c","d"]],[["e","f","g","z"],["h","i","j"]],[["k","l"],["m","n"]]];
let result = arrayA.reduce((c, v) => { v.forEach((o, i) => { c[i] = c[i] || [0]; c[i][0] += o.length; }) return c;}, []);
console.log(result);

How to merge subarray values and generate a 1-dimensional array of unique values?

In your desired output indexes are same, you never achieve that. because same indexes are over-written by most recent values.

You can get like below:-

$final_array = array_unique(call_user_func_array('array_merge', $array)); //convert multi-dimensional array to single dimensional and remove duplicates
asort($final_array); // sort by value. this is optional
$final_array = array_values($final_array); // re-index final array and this is optional too
echo "<pre/>";print_r($final_array); // print final array

Output:- https://eval.in/752750

merge all arrays with same title

This is not perfomance optimized, but shows a simple solution.

Collect all data grouped by title, then reformat the array to your expected result.

$array = json_decode($json, true);
$collect = [];
foreach($array as $item) {
$collect[$item['title']][] = $item['data'];
}
ksort($collect);

$titles = [];
foreach($collect as $title => $data) {
$names = array_column($data, 'name');
array_multisort($names, SORT_ASC, SORT_STRING, $data);
$titles[] = ['title' => $title, 'data' => $data];
}

echo json_encode($titles, JSON_PRETTY_PRINT); results in

[
{
"title": "A",
"data": [
{
"id": "317",
"name": "Aesethetica"
},
{
"id": "318",
"name": "Astonos"
}
]
},
{
"title": "B",
"data": [
{
"id": "321",
"name": "Bioderma"
},
{
"id": "320",
"name": "Bourjois"
}
]
}
]


Related Topics



Leave a reply



Submit