如何从数据库的JSON字符串数据生成有效的数组

时间:2019-09-06 10:25:47

标签: php arrays codeigniter-3

我的数据库中存储了一个JSON字符串

[{"Dish_ID":"99","Dish_Name":"Luke Warm Water (Glass-ml)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"40","Dish_Name":"Anwla juice(Glass-ml)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"42","Dish_Name":"Aloevera juice(Glass-ml)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"100","Dish_Name":"Methi Kadha(Cup-ml)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"147","Dish_Name":"Herbal Tea(Cup-ml)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"7","Dish_Name":"Sprout(-Bowl)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"8","Dish_Name":"Steam sprout(-bowl)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"22","Dish_Name":"Upma(-bowl)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"21","Dish_Name":"Uttapam(-Pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"71","Dish_Name":"Veg chilla(Plate-Piece)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"73","Dish_Name":"Suji chilla(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"69","Dish_Name":"Dhokla(Plate-Piece)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"88","Dish_Name":"Veg poha (Plate-gm)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"76","Dish_Name":"Fruit salad(-BOWL)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"43","Dish_Name":"Apple(Plate-Piece)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"44","Dish_Name":"Orange(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"45","Dish_Name":"Papaya(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"46","Dish_Name":"Guava(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"50","Dish_Name":"Pear(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"62","Dish_Name":"Kakadi(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"63","Dish_Name":"Cucumber(Plate-Piece)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"64","Dish_Name":"Mausami(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"65","Dish_Name":"Grapes(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"66","Dish_Name":"Malta(-pcs.)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"81","Dish_Name":"Black grapes(Bowl-gm)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"80","Dish_Name":"Falsa(-BOWL)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"82","Dish_Name":"Kiwis(-PCS)","Diet_Type":"Suggested","Quantity":"1"},{"Dish_ID":"40","Dish_Name":"Anwla juice(Glass-ml)","Diet_Type":"Focus","Quantity":"1"},{"Dish_ID":"42","Dish_Name":"Aloevera juice(Glass-ml)","Diet_Type":"Focus","Quantity":"1"},{"Dish_ID":"100","Dish_Name":"Methi Kadha(Cup-ml)","Diet_Type":"Focus","Quantity":"1"},{"Dish_ID":"7","Dish_Name":"Sprout(-Bowl)","Diet_Type":"Focus","Quantity":"1"},{"Dish_ID":"2","Dish_Name":"Tea(-cup)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"1","Dish_Name":"Biscuit(Plate-Piece)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"3","Dish_Name":"Bread(Plate-Piece)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"120","Dish_Name":"BUTTER(Spoon-gm)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"121","Dish_Name":"JAM(Spoon-gm)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"122","Dish_Name":"Coffee(Cup-ml)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"211","Dish_Name":"Fast Food(Plate-Piece)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"156","Dish_Name":"Packed Juices (Glass-ml)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"160","Dish_Name":"Oily Foods(Plate-gm)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"159","Dish_Name":"Refined Flour(Plate-gm)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"162","Dish_Name":"Sweets(Plate-Piece)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"155","Dish_Name":"Beverages(Plate-Piece)","Diet_Type":"Restricted","Quantity":"0"},{"Dish_ID":"157","Dish_Name":"Spreaders (Plate-gm)","Diet_Type":"Restricted","Quantity":"0"},{"time":"09:30 AM"}]

使用json_decode('json_string',TRUE)函数将其解码为数组时,我得到的是有效数组

Array
(
    [0] => Array
        (
            [Dish_ID] => 99
            [Dish_Name] => Luke Warm Water (Glass-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [1] => Array
        (
            [Dish_ID] => 40
            [Dish_Name] => Anwla juice(Glass-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [2] => Array
        (
            [Dish_ID] => 42
            [Dish_Name] => Aloevera juice(Glass-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [3] => Array
        (
            [Dish_ID] => 100
            [Dish_Name] => Methi Kadha(Cup-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [4] => Array
        (
            [Dish_ID] => 147
            [Dish_Name] => Herbal Tea(Cup-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [5] => Array
        (
            [Dish_ID] => 7
            [Dish_Name] => Sprout(-Bowl)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [6] => Array
        (
            [Dish_ID] => 8
            [Dish_Name] => Steam sprout(-bowl)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [7] => Array
        (
            [Dish_ID] => 22
            [Dish_Name] => Upma(-bowl)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [8] => Array
        (
            [Dish_ID] => 21
            [Dish_Name] => Uttapam(-Pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [9] => Array
        (
            [Dish_ID] => 71
            [Dish_Name] => Veg chilla(Plate-Piece)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [10] => Array
        (
            [Dish_ID] => 73
            [Dish_Name] => Suji chilla(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [11] => Array
        (
            [Dish_ID] => 69
            [Dish_Name] => Dhokla(Plate-Piece)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [12] => Array
        (
            [Dish_ID] => 88
            [Dish_Name] => Veg poha (Plate-gm)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [13] => Array
        (
            [Dish_ID] => 76
            [Dish_Name] => Fruit salad(-BOWL)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [14] => Array
        (
            [Dish_ID] => 43
            [Dish_Name] => Apple(Plate-Piece)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [15] => Array
        (
            [Dish_ID] => 44
            [Dish_Name] => Orange(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [16] => Array
        (
            [Dish_ID] => 45
            [Dish_Name] => Papaya(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [17] => Array
        (
            [Dish_ID] => 46
            [Dish_Name] => Guava(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [18] => Array
        (
            [Dish_ID] => 50
            [Dish_Name] => Pear(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [19] => Array
        (
            [Dish_ID] => 62
            [Dish_Name] => Kakadi(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [20] => Array
        (
            [Dish_ID] => 63
            [Dish_Name] => Cucumber(Plate-Piece)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [21] => Array
        (
            [Dish_ID] => 64
            [Dish_Name] => Mausami(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [22] => Array
        (
            [Dish_ID] => 65
            [Dish_Name] => Grapes(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [23] => Array
        (
            [Dish_ID] => 66
            [Dish_Name] => Malta(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [24] => Array
        (
            [Dish_ID] => 81
            [Dish_Name] => Black grapes(Bowl-gm)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [25] => Array
        (
            [Dish_ID] => 80
            [Dish_Name] => Falsa(-BOWL)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [26] => Array
        (
            [Dish_ID] => 82
            [Dish_Name] => Kiwis(-PCS)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [27] => Array
        (
            [Dish_ID] => 40
            [Dish_Name] => Anwla juice(Glass-ml)
            [Diet_Type] => Focus
            [Quantity] => 1
        )

    [28] => Array
        (
            [Dish_ID] => 42
            [Dish_Name] => Aloevera juice(Glass-ml)
            [Diet_Type] => Focus
            [Quantity] => 1
        )

    [29] => Array
        (
            [Dish_ID] => 100
            [Dish_Name] => Methi Kadha(Cup-ml)
            [Diet_Type] => Focus
            [Quantity] => 1
        )

    [30] => Array
        (
            [Dish_ID] => 7
            [Dish_Name] => Sprout(-Bowl)
            [Diet_Type] => Focus
            [Quantity] => 1
        )

    [31] => Array
        (
            [Dish_ID] => 2
            [Dish_Name] => Tea(-cup)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [32] => Array
        (
            [Dish_ID] => 1
            [Dish_Name] => Biscuit(Plate-Piece)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [33] => Array
        (
            [Dish_ID] => 3
            [Dish_Name] => Bread(Plate-Piece)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [34] => Array
        (
            [Dish_ID] => 120
            [Dish_Name] => BUTTER(Spoon-gm)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [35] => Array
        (
            [Dish_ID] => 121
            [Dish_Name] => JAM(Spoon-gm)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [36] => Array
        (
            [Dish_ID] => 122
            [Dish_Name] => Coffee(Cup-ml)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [37] => Array
        (
            [Dish_ID] => 211
            [Dish_Name] => Fast Food(Plate-Piece)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [38] => Array
        (
            [Dish_ID] => 156
            [Dish_Name] => Packed Juices (Glass-ml)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [39] => Array
        (
            [Dish_ID] => 160
            [Dish_Name] => Oily Foods(Plate-gm)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [40] => Array
        (
            [Dish_ID] => 159
            [Dish_Name] => Refined Flour(Plate-gm)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [41] => Array
        (
            [Dish_ID] => 162
            [Dish_Name] => Sweets(Plate-Piece)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [42] => Array
        (
            [Dish_ID] => 155
            [Dish_Name] => Beverages(Plate-Piece)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [43] => Array
        (
            [Dish_ID] => 157
            [Dish_Name] => Spreaders (Plate-gm)
            [Diet_Type] => Restricted
            [Quantity] => 0
        )

    [44] => Array
        (
            [time] => 09:30 AM
        )

)

并希望从我们所有的阵列中过滤所有建议的饮食

$breakfast_suggested = array_filter($breakfast,function($k){
            return $k['Diet_Type'] == "Suggested";
        }); 

这次我遇到错误

A PHP Error was encountered
Severity: Notice

Message: Undefined index: Diet_Type

在打印$ breakfast_时,建议此打印正常

Array
(
    [0] => Array
        (
            [Dish_ID] => 99
            [Dish_Name] => Luke Warm Water (Glass-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [1] => Array
        (
            [Dish_ID] => 40
            [Dish_Name] => Anwla juice(Glass-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [2] => Array
        (
            [Dish_ID] => 42
            [Dish_Name] => Aloevera juice(Glass-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [3] => Array
        (
            [Dish_ID] => 100
            [Dish_Name] => Methi Kadha(Cup-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [4] => Array
        (
            [Dish_ID] => 147
            [Dish_Name] => Herbal Tea(Cup-ml)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [5] => Array
        (
            [Dish_ID] => 7
            [Dish_Name] => Sprout(-Bowl)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [6] => Array
        (
            [Dish_ID] => 8
            [Dish_Name] => Steam sprout(-bowl)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [7] => Array
        (
            [Dish_ID] => 22
            [Dish_Name] => Upma(-bowl)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [8] => Array
        (
            [Dish_ID] => 21
            [Dish_Name] => Uttapam(-Pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [9] => Array
        (
            [Dish_ID] => 71
            [Dish_Name] => Veg chilla(Plate-Piece)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [10] => Array
        (
            [Dish_ID] => 73
            [Dish_Name] => Suji chilla(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [11] => Array
        (
            [Dish_ID] => 69
            [Dish_Name] => Dhokla(Plate-Piece)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [12] => Array
        (
            [Dish_ID] => 88
            [Dish_Name] => Veg poha (Plate-gm)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [13] => Array
        (
            [Dish_ID] => 76
            [Dish_Name] => Fruit salad(-BOWL)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [14] => Array
        (
            [Dish_ID] => 43
            [Dish_Name] => Apple(Plate-Piece)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [15] => Array
        (
            [Dish_ID] => 44
            [Dish_Name] => Orange(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [16] => Array
        (
            [Dish_ID] => 45
            [Dish_Name] => Papaya(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [17] => Array
        (
            [Dish_ID] => 46
            [Dish_Name] => Guava(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [18] => Array
        (
            [Dish_ID] => 50
            [Dish_Name] => Pear(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [19] => Array
        (
            [Dish_ID] => 62
            [Dish_Name] => Kakadi(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [20] => Array
        (
            [Dish_ID] => 63
            [Dish_Name] => Cucumber(Plate-Piece)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [21] => Array
        (
            [Dish_ID] => 64
            [Dish_Name] => Mausami(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [22] => Array
        (
            [Dish_ID] => 65
            [Dish_Name] => Grapes(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [23] => Array
        (
            [Dish_ID] => 66
            [Dish_Name] => Malta(-pcs.)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [24] => Array
        (
            [Dish_ID] => 81
            [Dish_Name] => Black grapes(Bowl-gm)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [25] => Array
        (
            [Dish_ID] => 80
            [Dish_Name] => Falsa(-BOWL)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

    [26] => Array
        (
            [Dish_ID] => 82
            [Dish_Name] => Kiwis(-PCS)
            [Diet_Type] => Suggested
            [Quantity] => 1
        )

)

所以我不明白这里发生了什么

0 个答案:

没有答案