从json删除不需要的数组

时间:2019-05-19 12:50:38

标签: php json laravel laravel-5 eloquent

我是Laravel的新手。我正在使用一个函数创建一个json结构。这是我当前的输出:

{
    "success": "1",
    "data": [
        {
            "category_type_id": 1,
            "category_type": "Study",
            "category_icon": "http://192.168.1.132:8000/images/category/study.svg"
        },
        {
            "category_type_id": 2,
            "category_type": "Sports",
            "category_icon": "http://192.168.1.132:8000/images/category/game.svg"
        },
        {
            "category_type_id": 3,
            "category_type": "Other",
            "category_icon": "http://192.168.1.132:8000/images/category/other.svg"
        }
    ]
}

这是我的控制器代码:

$get_all_category = CategoryType::all();

return response()->json(['success' => '1', 'data' => $get_all_category]);

我想要没有从数据plz启动的数组的结果需要解决方案

1 个答案:

答案 0 :(得分:0)

只需从json响应中删除您的其他属性:

$get_all_category = CategoryType::all();
return response()->json($get_all_category);

这将返回您的json:

[ 
    { 
        "category_type_id": 1,
        "category_type": "Study",
        "category_icon": "http://192.168.1.132:8000/images/category/study.svg"
    },
    {
        "category_type_id": 2,
        "category_type": "Sports",
        "category_icon": "http://192.168.1.132:8000/images/category/game.svg" },
    {
        "category_type_id": 3
        "category_type": "Other",
        "category_icon": "http://192.168.1.132:8000/images/category/other.svg"
     }
]

如果要保留成功属性,可以执行以下操作,但这只会将要删除的旧数据属性更改为“ 0”:

$get_all_category = CategoryType::all();
return response()->json(['success' =>'1', $get_all_category]);