2017-05-30 57 views
2

我一直在嘗試在我的應用程序中創建一個文件夾概念。
我試圖以APi的json格式返回o/p。 我想列出文件夾,子文件夾和子文件夾中的文件,但是當我試圖嵌套數組這樣YII2中的多維數組

{ 
    "success_msg": 1, 
    "message": "success", 
    "details": [ 
    { 
     "foldername": "Test_folder_1", 
     "subfolder": [ 
     { 
      "sub_folder_name": "Test_sub_folder_1", 

     "subfile": [ 
     { 
      "filename": "test_1.pdf" 
     }, 
     { 
      "filename": "test_2.pdf" 
     } 
     ] 
     }, 
     { 
      "sub_folder_name": "test_folder", 
"subfile": [ 
     { 
      "filename": "test_3.jpg" 
     } 
     ] 
     } 
     ] 
    } 
    ] 
} 

我得到錯誤的O/p.Let我跟大家分享的是

{ 
    "success_msg": 1, 
    "message": "success", 
    "details": [ 
    { 
     "foldername": "Test_folder_1", 
     "subfolder": [ 
     { 
      "sub_folder_name": "Test_sub_folder_1" 
     }, 
     { 
      "sub_folder_name": "test_folder" 
     } 
     ], 
     "subfile": [ 
     { 
      "filename": "test_1.pdf" 
     }, 
     { 
      "filename": "test_2.pdf" 
     }, 
     { 
      "filename": "test_3.jpg" 
     } 
     ] 
    }, 
    { 
     "foldername": "sample_folder", 
     "subfolder": [], 
     "subfile": [] 
    } 
    ] 
} 

這是我的行動

public function actionFiles() { 
    ob_start(); 
    $raw = file_get_contents("php://input"); 
    $obj = json_decode($raw); 
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 
    if ($obj!=null){ 
     $user_id   = $obj->cur_user_id; 
     $user_model   = User::find()->where(['id'=>$user_id])->one(); 
     $developer_id  = $user_model->createdby; 
     $folder_model  = Folder::find()->where(['developerid'=>$developer_id,'foldertype'=>'parent'])->all(); 
     $count_folder  = count($folder_model); 
     if($count_folder > '0'){ 
      $response = ['success_msg' => 1, 'message' => 'success', 'details' => []]; 
      foreach($folder_model as $folder) { 
       $folder_id       = $folder['id']; 
       $userdet['foldername']    = $folder['foldername']; 
       $sub = []; 
       $sub_files = []; 
      $subfolder_model = Folder::find()->where(['folderid'=>$folder_id])->all(); 
      foreach ($subfolder_model as $subfolder) { 
       $sub_folder_id    = $subfolder['id']; 
       $sub_name['sub_folder_name'] = $subfolder['foldername']; 
      $sub_file = Files::find()->where(['folderid'=>$folder_id,'subfolderid'=>$sub_folder_id])->all(); 
      foreach ($sub_file as $file) { 
       $filename['filename'] = $file['filename']; 
       array_push($sub_files, $filename); 
      } 
       array_push($sub, $sub_name);     
       // echo '<pre>';print_r($userdet); 
      } 

       $userdet['subfolder'] = $sub;    
       $userdet['subfile'] = $sub_files; 
       array_push($response['details'], $userdet); 

      } 
      echo json_encode($response); 
     }else { 
       // failed to insert row 
      $response["success_msg"] = 2; 
      $response["message"] = "No News found"; 
      echo json_encode($response); 
     } 

    } else { 
     $response["success_msg"] = 3; 
     $response["message"] = "Data missing"; 
     echo json_encode($response); 
    } 
} 

誰能告訴我怎麼可以插入子文件夾排列子文件陣......在advanc
感謝e ..

回答

0

我更新了你的if文件夾存在條件。更新你的代碼並檢查它。

if($count_folder > '0'){ 
    $response = ['success_msg' => 1, 'message' => 'success', 'details' => []]; 

    $folder_tree = []; 
    foreach($folder_model as $folder) { 
     $folder_id       = $folder['id']; 

     $sub = []; 
     $subfolder_model = Folder::find()->where(['folderid'=>$folder_id])->all(); 
     foreach ($subfolder_model as $subfolder) { 
      $sub_folder_id    = $subfolder['id']; 

      $sub_files = []; 
      $sub_file = Files::find()->where(['folderid'=>$folder_id,'subfolderid'=>$sub_folder_id])->all(); 
      foreach ($sub_file as $file) { 
       array_push($sub_files, array('filename'=> $file['filename'])); 
      } 
      array_push($sub, array('sub_folder_name'=> $subfolder['foldername'], 'subfile'=> $sub_files));     
     } 

     array($folder_tree, array('foldername'=> $folder['foldername'], 'subfolder'=> $sub)); 
    } 
    $response['details'] = $folder_tree; 

    echo json_encode($response); 
} 

注: - 我沒有測試它,請檢查您的系統。

+0

謝謝支持。我想出了我的解決方案。讓我分享它 –

0

我更新了數組的位置的一些變化的代碼。

if ($count_folder > '0') { 
       $response = ['success_msg' => 1, 'message' => 'success', 'details' => []]; 
       foreach ($folder_model as $folder) { 
        $folder_id    = $folder['id']; 
        $userdet['foldername'] = $folder['foldername']; 
        $sub     = []; 
        $subfolder_model  = Folder::find()->where(['folderid' => $folder_id])->all(); 
        foreach ($subfolder_model as $subfolder) { 
        $sub_files    = []; 
         $sub_folder_id     = $subfolder['id']; 
         $sub_name['sub_folder_name'] = $subfolder['foldername']; 
         $sub_file = Files::find()->where(['folderid' => $folder_id, 'subfolderid' => $sub_folder_id])->all(); 
          echo '<pre>';print_r($sub_file); 

         foreach ($sub_file as $file) { 
          $filename['filename']  = Yii::$app->urlManager->createAbsoluteUrl('uploads/' . $file['filename']); 
          array_push($sub_files, $filename); 
         } 
         $sub_name['subfile']   = $sub_files; 
         array_push($sub, $sub_name); 
        } 

        $userdet['subfolder'] = $sub; 
        array_push($response['details'], $userdet); 
       } 
       echo json_encode($response);