数组算法

WBOY
Release: 2016-06-13 12:15:35
Original
907 people have browsed it

求一个数组算法
根据 “数组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 />)
Copy after login



------解决思路----------------------
$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);
Copy after login
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 />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template