本帖最后由 kk86868058 于 2013-04-09 21:10:27 编辑

解决方案 »

  1.   

    $result = array(
        'x' => 'xxx',
        'y' => 'yyy',
    );
    $aList = array(55,99,77,44);
    $bList = array('1-20', '3-55', '88-99', '40-45');$last = array();
    $f = array();
    foreach(array_reverse($aList, true) as $k=>$id){
        $last = array('id'=>$id, 'percent'=>$bList[$k], 'other'=>$last);
    }
    // 若想去除最底的other=>array(),需要你在foreach中判断一下
    $result['other'] = $last;
      

  2.   


    $result = array(
        'x' => 'xxx',
        'y' => 'yyy',
    );
    $aList = array(55,99,77,44);
    $bList = array('1-20', '3-55', '88-99', '40-45');
    function listMerge($aList = array(), $bList = array()){
    if(count($aList) != count($bList)){
    return false;
    }
    $newList = array();
    if(!empty($aList) && !empty($bList)){
    $newList['id'] = $aList[0];
    $newList['pcenter'] = $bList[0];
    array_shift($aList);
    array_shift($bList);
    $newList['other'] = listMerge($aList, $bList);
    }
    return $newList;

    }
    $result['other'] = listMerge($aList, $bList);
    echo "<pre>";
    print_r($result);die;楼上给出了一种方法,这是一种递归的方法。比楼上复杂一些...供参考。同样,想去除最底的other=>array(),需要你在判断一下
      

  3.   

    谢谢..我自己也完成了        $result['other'] = array();
            $curPoint = &$result['other'];
            for($i = 0; $i < count($aList); $i++){
                $resultItem = array();
                $resultItem ['id'] = $aList[$i];
                $resultItem ['percent'] = $bList[$i];            $curPoint = $resultItem;
                if(isset($aList[$i+1])){
                    $curPoint = &$curPoint['_complex'];
                }
            }谢谢...坑爹的需求继续在变,继续给另一需求写算法.睡个觉再管.