js让两个一维数组的值合并

JavaScript016

js让两个一维数组的值合并,第1张

function deepObjectMerge(FirstOBJ, SecondOBJ) { // 深度合并对象

    for (var key in SecondOBJ) {

        FirstOBJ[key] = FirstOBJ[key] && FirstOBJ[key].toString() === "[object Object]" ?

            deepObjectMerge(FirstOBJ[key], SecondOBJ[key]) : FirstOBJ[key] = SecondOBJ[key]

    }

    return FirstOBJ

}

php数组根据某一个键值,把相同键值的合并生成一个新的二维数组

源数据:

$infos = array( array( 'a' =>36, 'b' =>'xa', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>3, 'b' =>'vd', 'c' =>'2015-08-20 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>6, 'b' =>'wwe', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>36, 'b' =>'se', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>6, 'b' =>'aw', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>36, 'b' =>'bv', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>12, 'b' =>'xx', 'c' =>'2015-08-27 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

)

)1234567891011121314151617181920212223242526272829303132333435363738394041424344

处理要求:将数组中a相同的二维数组合并到一个数组中,生成一个新的数组

代码:

<?php

$result= array()foreach ($infos as $key =>$info) { $result[$info['a']][] = $info

}

print_r($result)123456

输出:

Array(

[36] =>Array

(

[0] =>Array

(

[a] =>36

[b] =>xa

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

[1] =>Array

(

[a] =>36

[b] =>se

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

[2] =>Array

(

[a] =>36

[b] =>bv

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

)

[3] =>Array

(

[0] =>Array

(

[a] =>3

[b] =>vd

[c] =>2015-08-20 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

)

[6] =>Array

(

[0] =>Array

(

[a] =>6

[b] =>wwe

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

[1] =>Array

(

[a] =>6

[b] =>aw

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

)

[12] =>Array

(

[0] =>Array

(

[a] =>12

[b] =>xx

[c] =>2015-08-27 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

)

)

描述

方法concat()将创建并返回一个新数组,这个数组是将所有参数都添加到array中生成的。它并不修改array。如果要进行concat()操作的参数是一个数组,那么添加的是数组中的元素,而不是数组。

示例

var

a

=

[1,2,3]

a.concat(4,

5)

//返回

[1,2,3,4,5]

a.concat([4,5])

//返回

[1,2,3,4,5]

a.concat([4,5],[6,7])

//返回

[1,2,3,4,5,6,7]

a.concat(4,

[5,[6,7]])

//返回

[1,2,3,4,5,[6,7]]