数组算法
程序员文章站
2022-06-10 12:51:07
...
求一个数组算法
根据 “数组1” 和 “数组2”,得到 “数组3”这样的结果,而且最好能高效点
数组2是设置好的一些属性;数组1是商品可能只填写了某个或某几个属性
------解决思路----------------------
根据 “数组1” 和 “数组2”,得到 “数组3”这样的结果,而且最好能高效点
数组2是设置好的一些属性;数组1是商品可能只填写了某个或某几个属性
//数组1
Array
(
[100008] => Array
(
[5] => Array
(
[name] => 价格
[0] => 不限
)
[6] => Array
(
[name] => 折扣
[0] => 不限
)
)
[100009] => Array
(
[5] => Array
(
[name] => 价格
[8] => 0-29
)
[6] => Array
(
[name] => 折扣
[13] => 3-5折
)
[7] => Array
(
[name] => 出版社
[20] => 北京大学出版社
)
)
)
//数组2
Array
(
[0] => Array
(
[attr_id] => 5
[attr_name] => 价格
[attr_value] => 0-29,30-49,50-99,100以上
)
[1] => Array
(
[attr_id] => 6
[attr_name] => 折扣
[attr_value] => 0-3折,3-5折,5-7折,7-10折
)
[2] => Array
(
[attr_id] => 7
[attr_name] => 出版社
[attr_value] =>
)
)
//得到结果:数组3
Array
(
[100008] => Array
(
[价格] => 不限
[折扣] => 不限
[出版社] =>
)
[100009] => Array
(
[价格] => 0-29
[折扣] => 3-5折
[出版社] => 北京大学出版社
)
)
------解决思路----------------------
$a = array (
100008 => array (
5 => array (
'name' => '价格',
0 => '不限',
),
6 => array (
'name' => '折扣',
0 => '不限',
),
),
100009 => array (
5 => array (
'name' => '价格',
8 => '0-29',
),
6 => array (
'name' => '折扣',
13 => '3-5折',
),
7 => array (
'name' => '出版社',
20 => '北京大学出版社',
),
),
);
$b = array (
0 => array (
'attr_id' => '5',
'attr_name' => '价格',
'attr_value' => '0-29,30-49,50-99,100以上',
),
1 => array (
'attr_id' => '6',
'attr_name' => '折扣',
'attr_value' => '0-3折,3-5折,5-7折,7-10折',
),
2 => array (
'attr_id' => '7',
'attr_name' => '出版社',
'attr_value' => '',
),
);
foreach($a as $key=>$item) {
$t = array();
foreach($b as $v) {
$k = $v['attr_id'];
$t[$v['attr_name']] = isset($item[$k]) ? end($item[$k]) : $v['attr_value'];
}
$c[$key] = $t;
}
print_r($c);
Array
(
[100008] => Array
(
[价格] => 不限
[折扣] => 不限
[出版社] =>
)
[100009] => Array
(
[价格] => 0-29
[折扣] => 3-5折
[出版社] => 北京大学出版社
)
)
相关文章
相关视频