首頁 >後端開發 >php教程 >帮忙转化一个数组,该怎么处理

帮忙转化一个数组,该怎么处理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原創
2016-06-13 10:45:33727瀏覽

帮忙转化一个数组
有一个这样结构的数组

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$arr = array(    array('goods_id'=>'1','sku_id'=>'001','zk'=>'0.5'),    array('goods_id'=>'1','sku_id'=>'002','zk'=>'0.5'),    array('goods_id'=>'1','sku_id'=>'006','zk'=>'0.5'),    array('goods_id'=>'2','sku_id'=>'003','zk'=>'0.8'),    array('goods_id'=>'2','sku_id'=>'004','zk'=>'0.8'),);

转化成
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$_arr = array(    '1' =>array(                'zk'=>'0.5',                'mx'=>array(                    array('sku_id'=>'001','zk'=>'0.5'),                    array('sku_id'=>'002','zk'=>'0.5'),                    array('sku_id'=>'006','zk'=>'0.5'),                ),    '2' =>array(                'zk'=>'0.8',                'mx'=>array(                    array('sku_id'=>'003','zk'=>'0.8'),                    array('sku_id'=>'004','zk'=>'0.8'),                ),    ));


------解决方案--------------------
PHP code
<?php $arr = array(    array('goods_id'=>'1','sku_id'=>'001','zk'=>'0.5'),    array('goods_id'=>'1','sku_id'=>'002','zk'=>'0.5'),    array('goods_id'=>'1','sku_id'=>'006','zk'=>'0.5'),    array('goods_id'=>'2','sku_id'=>'003','zk'=>'0.8'),    array('goods_id'=>'2','sku_id'=>'004','zk'=>'0.8'),);foreach($arr as $v){    $tmp[$v['zk']]['zk']=$v['zk'];    $tmp[$v['zk']]['mx'][]=array_splice($v,1,2);}print_r(array_values($tmp));<div class="clear">
                 
              
              
        
            </div>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn