js一个一维数组和一个多维数组的key和键值相同进行合并,结果跟C一样。
"A":{"items":[{"id":"logo","css":"image_x"},{"css":"col_2","items":[{"id":"name","css...
"A":{ "items": [
{"id": "logo", "css": "image_x"},
{"css": "col_2", "items": [
{"id": "name", "css": "title_l"},
{"id": "id", "css": "body_r"}
]},
{"css": "col_1", "items": [
{"id": "phone", "css": "body_l"}
]}
]
}
"B":[
{"id": 111, "name": "name 111", "phone": "2234455", "logo": "aaa.gif"},
{"id": 222, "name": "name 222", "phone": "4566677", "logo": "bbb.gif"}
]
/**
* A和B数组合并 = 就是B数组的key==A数组的键值,就把B对象的键值给A数组的键值匹配的对象
* 最后结果生成如下 C 数组
*/*
"C":[
{"img": {"id": "aaa.gif", "css": "image_x"}, "items": [
{"css": "col_2", "items": [
{"id": "name 111", "css": "title_l"},
{"id": 111, "css": "body_r"}
]},
{"css": "col_1", "items": [
{"id": "2234455", "css": "body_l"}
]}
]},
{"img": {"id": "bbb.gif", "css": "image_x"}, "items": [
{"css": "col_2", "items": [
{"id": "name 222", "css": "title_l"},
{"id": 222, "css": "body_r"}
]},
{"css": "col_1", "items": [
{"id": "4566677", "css": "body_l"}
]}
]}
] 展开
{"id": "logo", "css": "image_x"},
{"css": "col_2", "items": [
{"id": "name", "css": "title_l"},
{"id": "id", "css": "body_r"}
]},
{"css": "col_1", "items": [
{"id": "phone", "css": "body_l"}
]}
]
}
"B":[
{"id": 111, "name": "name 111", "phone": "2234455", "logo": "aaa.gif"},
{"id": 222, "name": "name 222", "phone": "4566677", "logo": "bbb.gif"}
]
/**
* A和B数组合并 = 就是B数组的key==A数组的键值,就把B对象的键值给A数组的键值匹配的对象
* 最后结果生成如下 C 数组
*/*
"C":[
{"img": {"id": "aaa.gif", "css": "image_x"}, "items": [
{"css": "col_2", "items": [
{"id": "name 111", "css": "title_l"},
{"id": 111, "css": "body_r"}
]},
{"css": "col_1", "items": [
{"id": "2234455", "css": "body_l"}
]}
]},
{"img": {"id": "bbb.gif", "css": "image_x"}, "items": [
{"css": "col_2", "items": [
{"id": "name 222", "css": "title_l"},
{"id": 222, "css": "body_r"}
]},
{"css": "col_1", "items": [
{"id": "4566677", "css": "body_l"}
]}
]}
] 展开
1个回答
展开全部
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
)
)
)
源数据:
$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
)
)
)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询