0

在此处输入图像描述

如何将此excel文件转换为以下数组格式

Array [Test's Restaurant] => Array
(
    [Appetizer] => Array
        (
            [Salsa & Chips] => Array
                (
                    [Radio] => Array
                        (
                            [0] => Coke
                            [1] => Sprite
                            [2] => Diet Coke
                            [3] => Diet Sprite
                        )

                    [Checkbox] => Array
                        (
                            [0] => Chocolate Cake
                            [1] => Vanilla Cake
                        )

                )

            [Salad] => Array
                (
                    [Radio] => Array
                        (
                            [0] => Italian
                            [2] => French
                            [3] => Ranch
                        )

                )

        )

    [Lunch Menu] => Array
        (
            [Burrito] => Array
                (
                    [Radio] => Array
                        (
                            [0] => Roll
                            [1] => Steak
                            [3] => Chicken
                        )

                    [Checkbox] => Array
                        (
                            [0] => Coke
                            [1] => Sprite
                            [3] => Diet Coke
                        )

                )
)

)
4

1 回答 1

0

我尝试过的代码

// 这给出了 phpExcel 库导入函数返回的主数组

$mdar = phpexcel_import($path);
arrange_array($mdar[0]);

function arrange_array($mdar){

    $mainarr = array();
    $arr = array();
    $j = count($mdar);

    for($a = 0 ; $a <=$j ; $a++ ){
        if(!empty($mdar[$a]['Menu Category']))
        {
            $b = next_point($mdar,$a,$j);
            $mainarr[$mdar[$a]['Menu Category']] = arrange_array2($mdar,$a,($b-1));
            $arr = $mainarr;
        }

    }

}

function next_point($mdar,$last,$j){
    //echo 'next_point $mdar '.$mdar.'next_point $last '.$last.' next_point $j '.$j .'<br>';
     for($a = $last ; $a <=$j ; $a++ ){
        if(!empty($mdar[$a]['Menu Category']))
        {
            $arr[] = $a;
        }
    }
    return $arr[1];
}

function arrange_array2($mdar,$sp,$lp){

    $arr = array();
    for($a = $sp ; $a <=$lp ; $a++ ){
        if(!empty($mdar[$a]['Menu Item']))
        {
            $b = next_point2($mdar,$a,$lp);
            $mainarr[$mdar[$a]['Menu Item']] = arrange_array3($mdar,$a,$b);
            $arr = $mainarr;
        }
    }
    return $arr;
}

function next_point2($mdar,$last,$j){
   // echo 'next_point2 $mdar '.$mdar.' next_point2 $last '.$last.' next_point2 $j '.$j .'<br>';
     for($a = $last ; $a <=$j ; $a++ ){
        if(!empty($mdar[$a]['Menu Item'])){
            $arr[] = $a;
        }
    }
    echo "next point 2 ";
    print_r($arr);

    return ($arr[1])?$arr[1]-1:$arr[0];
}

function arrange_array3($mdar,$sp,$lp){
   //echo 'next_point2 $mdar '.$mdar.' next_point2 $sp '.$sp.' next_point2 $j '.$lp .'<br>';
    $arr = array();
    for($a = $sp ; $a <=$lp ; $a++){
        if(!empty($mdar[$a]['Menu Item option type']))
        {
            $b = next_point3($mdar,$a,$lp);
            $mainarr[$mdar[$a]['Menu Item option type']] = arrange_array4($mdar,$a,$b);
            $arr = $mainarr;
        }
    }

    return $arr;
}

function next_point3($mdar,$last,$j){
   // echo 'next_point2 $mdar '.$mdar.' next_point2 $last '.$last.' next_point2 $j '.$j .'<br>';
     for($a = $last ; $a <=$j ; $a++ ){
        if(!empty($mdar[$a]['Menu Item option type']))
        {
            $arr[] = $a;
        }
    }
    return ($arr[1])?$arr[1]-1:$arr[0];
}
function arrange_array4($mdar,$sp,$lp){

    for($a = $sp ; $a <=$lp ; $a++ ){
        if(!empty($mdar[$a]['Menu Option Values']))
        {
            $arr[] = $mdar[$a]['Menu Option Values'];
        }
    }

    return $arr;

}
于 2013-11-01T08:03:36.717 回答