数组算法

WBOY
Lepaskan: 2016-06-13 12:15:35
asal
907 orang telah melayarinya

求一个数组算法
根据 “数组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 />)
Salin selepas log masuk



------解决思路----------------------
$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);
Salin selepas log masuk
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 />
Salin selepas log masuk

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan