查找空白数组并取消设置/删除对象

问题描述 投票:0回答:1

我被困在Laravel,需要专家的帮助才能解决此问题。

我有多维数组,正在开发api。我试图取消设置,但不知何故引发了错误。我也尝试过使用array_filter,但无法解决该问题。

$data = 
    Array
(
    [one] => oned
    [two] => 222
    [three] => three
    [four] => 444
    [five] => 555
    [events] => Array
        (
            [0] => Array
                (
                    [feed] => Array
                        (
                            [0] => Array
                                (
                                    [date-time] => 191018080000
                                    [sub-type] => abc
                                    [comments] => test comments
                                    [parameter-list] => para1, para2
                                    [state] => bad
                                    [value] => 1000
                                )

                        )

                )

        )

)
Array
(
    [one] => oned
    [two] => 2224564
    [three] => three
    [four] => 4444564
    [five] => 555456
    [events] => Array
        (
            [0] => Array
                (
                    [busfeed] => Array
                        (
                            [0] => Array
                                (
                                    [date-time] => 191019091000
                                    [sub-type] => abc
                                    [comments] => testing commnets
                                    [parameter-list] => buspara
                                    [state] => warning
                                    [value] => 55.22
                                )

                        )

                )

            [1] => Array
                (
                    [busfeed] => Array
                        (
                            [0] => Array
                                (
                                    [date-time] => 191019091000
                                    [sub-type] => abc
                                    [comments] => test
                                    [parameter-list] => parat
                                    [state] => warning
                                    [value] => 2.1425
                                )

                        )

                )

            [2] => Array
                (
                    [busfeed] => Array
                        (
                            [0] => Array
                                (
                                    [date-time] => 191019091000
                                    [sub-type] => abc
                                    [comments] => testing
                                    [parameter-list] => para
                                    [state] => warning
                                    [value] => 2054.1875
                                )

                        )

                )

        )

)
Array
(
    [one] => oned
    [two] => 222456
    [three] => three
    [four] => 444456
    [five] => 555456
    [events] => Array
        (
        )

)
Array
(
    [one] => oned
    [two] => 2226546
    [three] => three
    [four] => 4444564
    [five] => 555456
    [events] => Array
        (
        )

)
Array
(
    [one] => oned
    [two] => 2222
    [three] => three
    [four] => 4444
    [five] => 5555
    [events] => Array
        (
        )

)

有一些[事件]是空白的,没有数组。所以我想删除它,并获得具有数据的唯一数组对象。我需要像forench或for loop这样在Loop中创建它。

响应json中的formate

return response()->json(['data'=>$data)]);
php arrays laravel multidimensional-array
1个回答
0
投票

您可以申请下一个foreach循环:

foreach($data as $ind=>$record){
     if(empty($record['events'])) unset($data[$ind]);
}
© www.soinside.com 2019 - 2024. All rights reserved.