首页 >后端开发 >php教程 >数组算法

数组算法

WBOY
WBOY原创
2016-06-13 12:15:35936浏览

求一个数组算法
根据 “数组1” 和 “数组2”,得到 “数组3”这样的结果,而且最好能高效点
数组2是设置好的一些属性;数组1是商品可能只填写了某个或某几个属性

//数组1<br />Array<br />(<br />    [100008] => Array<br />        (<br />            [5] => Array<br />                (<br />                    [name] => 价格<br />                    [0] => 不限<br />                )<br /><br />            [6] => Array<br />                (<br />                    [name] => 折扣<br />                    [0] => 不限<br />                )<br /><br />        )<br /><br />    [100009] => Array<br />        (<br />            [5] => Array<br />                (<br />                    [name] => 价格<br />                    [8] => 0-29<br />                )<br /><br />            [6] => Array<br />                (<br />                    [name] => 折扣<br />                    [13] => 3-5折<br />                )<br />	   [7] => Array<br />                (<br />                    [name] => 出版社<br />                    [20] => 北京大学出版社<br />                )<br /><br />        )<br /><br />)<br /><br />//数组2<br />Array<br />(<br />    [0] => Array<br />        (<br />            [attr_id] => 5<br />            [attr_name] => 价格<br />            [attr_value] => 0-29,30-49,50-99,100以上<br />        )<br /><br />    [1] => Array<br />        (<br />            [attr_id] => 6<br />            [attr_name] => 折扣<br />            [attr_value] => 0-3折,3-5折,5-7折,7-10折<br />        )<br /><br />    [2] => Array<br />        (<br />            [attr_id] => 7<br />            [attr_name] => 出版社<br />            [attr_value] =><br />        )<br /><br />)<br /><br />//得到结果:数组3<br />Array<br />(<br />    [100008] => Array<br />        (<br />            [价格] => 不限<br />            [折扣] => 不限<br />            [出版社] => <br />        )<br /><br />    [100009] => Array<br />        (<br />            [价格] => 0-29<br />            [折扣] => 3-5折<br />            [出版社] => 北京大学出版社<br />        )<br /><br />)


 
------解决思路----------------------
$a = array (<br />  100008 => array (<br />    5 => array (<br />      'name' => '价格',<br />      0 => '不限',<br />    ),<br />    6 => array (<br />      'name' => '折扣',<br />      0 => '不限',<br />    ),<br />  ),<br />  100009 => array (<br />    5 => array (<br />      'name' => '价格',<br />      8 => '0-29',<br />    ),<br />    6 => array (<br />      'name' => '折扣',<br />      13 => '3-5折',<br />    ),<br />    7 => array (<br />      'name' => '出版社',<br />      20 => '北京大学出版社',<br />    ),<br />  ),<br />);<br /><br />$b = array (<br />  0 => array (<br />    'attr_id' => '5',<br />    'attr_name' => '价格',<br />    'attr_value' => '0-29,30-49,50-99,100以上',<br />  ),<br />  1 => array (<br />    'attr_id' => '6',<br />    'attr_name' => '折扣',<br />    'attr_value' => '0-3折,3-5折,5-7折,7-10折',<br />  ),<br />  2 => array (<br />    'attr_id' => '7',<br />    'attr_name' => '出版社',<br />    'attr_value' => '',<br />  ),<br />);<br /><br />foreach($a as $key=>$item) {<br />  $t = array();<br />  foreach($b as $v) {<br />    $k = $v['attr_id'];<br />    $t[$v['attr_name']] = isset($item[$k]) ? end($item[$k]) : $v['attr_value'];<br />  }<br />  $c[$key] = $t;<br />}<br />print_r($c);
Array<br />(<br />    [100008] => Array<br />        (<br />            [价格] => 不限<br />            [折扣] => 不限<br />            [出版社] => <br />        )<br /><br />    [100009] => Array<br />        (<br />            [价格] => 0-29<br />            [折扣] => 3-5折<br />            [出版社] => 北京大学出版社<br />        )<br /><br />)<br /><br />

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn