How php merges multi-dimensional array Subsets

Source: Internet
Author: User
For example, a multi-dimensional array {code...}. I want to merge all the subset arrays of $ arr into a new array {code...}. What is the fastest way? For example, a multi-dimensional array?

$arr = array(    '0'=>array('1','2'),    '1'=>array('2','3'),    '2'=>array('3','4'),    '3'=>array('4','5'),    '4'=>array('5','6'),)

I want to merge all the subset arrays of $ arr into a new array.

$new_arr = array('1','2','2','3','3','4','4','5','5','6');

What is the fastest way?

Reply content:

For example, a multi-dimensional array

$arr = array(    '0'=>array('1','2'),    '1'=>array('2','3'),    '2'=>array('3','4'),    '3'=>array('4','5'),    '4'=>array('5','6'),)

I want to merge all the subset arrays of $ arr into a new array.

$new_arr = array('1','2','2','3','3','4','4','5','5','6');

What is the fastest way?

array_walkVersion 1 code:

$res = array();array_walk($arr, function($item, $key) use (&$res) {$res = array_merge($res, $item);});

array_walk_recursiveVersion 2 code:

$res = array();array_walk_recursive($arr, function($item, $key) use (&$res) {$res[] = $item;});

The beautiful writing method requires the support of the PHP version. If not, the anonymous function anduseChange to common functions andglobal.

Supplementary answer:

foreachCode 3 of the General loop edition:

$res = array();foreach($arr as $item) {$res = array_merge($res, $item);}

double foreachNested loop common version code 4:

See @ thbourlove's answer.

Now that the subject emphasizes the time, I will make a careful evaluation. In order to increase the comparability, I wrote a general version that uses the third version of the loop as the comparison code, the following table lists the time consumed by the four groups of codes in different array lengths:

array_walk array_walk_recursive foreach double foreach
500 0.12 0.003 0.067 0.003
100 0.45 0.006 0.331 0.003
1500 1.7 0.005 1.523 0.003
2000 3.38 0.005 2.092 0.003
2500 6.16 0.008 4.126 0.004
3000 10.15 0.010 6.258 0.005

* Note 1:The left side of the table is the length of the array. The default value is the first-level array. For example, the length of the array in the question is 5. The length of each second-level array is the same as 2 in the example.
* Note 2:The default unit of data in the table is second.
* Note 3:Originally, the test data is an array of 100000 characters in length.array_walkIt took me 204 s to give up that long.
* NOTE 4:double foreachFor the code, see @ thbourlove's answer. In addition, the calculation time of this algorithm does not exist because the data compared with the previous three, so I found a group of data with the same specifications and the second algorithm for the same-period test, and obtained the current data in the table based on the relationship between the two groups of data.

Based on the above data, we can see that the nested loop of @ thbourlove is the highest in terms of performance. However, the personal control is the code aesthetic control, so the second type is preferred.

The above data can be used to obtain three methods:array_walk_recursiveThe method is undoubtedly the fastest.

array_walk_recursiveI would like to add the following:

$res = array();function merge($item, $key) {    global $res;    $res[] = $item}array_walk_recursive($arr, 'merge');

As for the Efficiency Test of the modified code, I will not do more here. It should be the same as the final result.

I had to step on @ Yi Hongyi's answer and found that my reputation was not enough. Well, I had to answer questions silently ..

In fact, the benchmark table in @ Yi Hongyi's answer shows that the algorithm complexity of solution 1 and solution 3 is O (mn ^ 2 ), the complexity of solution 2 is only O (mn ). M indicates the length of each list in the hash table, and n indicates the length of the hash table.

What has caused such a gap? With a closer look at the code, we will find that a function appears in both solution 1 and solution 3.array_merge. What did this product do?

In fact, each timearray_mergeThe operation merges the two arrays in the parameter, generates a new array, and allocates memory for it. The complexity of this operation is O (mn), while$res[] = $itemIn this way, the append to the array has only the complexity of O (1), and only O (m) is accumulated ). So the benchmark result can be understood.

Finally, you can try the following code:

$res = [];foreach ($arr as $item) {    foreach ($item as $value) {        $res[] = $item;    }}

In theory, it must be better than array_walk_recursive. I didn't test it. You can try it.

PS:array_walkSeries functions (includingarray_walk array_map array_filter......) I think it is slower than foreach, at least in php5.4.6. You are welcome to make a brick.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.