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 ..
謝謝支持。我想出了我的解決方案。讓我分享它 –