2013-11-01 38 views
0

enter image description here更改Excel數據導入多維數組

我如何轉換這個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 
         ) 

       ) 
) 

) 
+0

您是否嘗試過任何沒有? – Sirko

+0

使用類似[PHPExcel](http://phpexcel.codeplex.com)的庫讀取Excel文件並使用庫toArray()方法;然後操作該數組以獲得所需的確切結構。 –

回答

0

,我已經試過的代碼,但

//這使主由phpExcel Library導入函數返回的數組

$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; 

}