猿问

无法在 Laravel 中使用 Illuminate\\Http\\JsonResponse

我有这样的数据集:


$data[] =

Array

(

    [u_d] => ABCDEF

    [0] => 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

                                    )


                            )


                    )


            )

        )



    [1] => Array

        (

            [one] => two

            [two] => 224562

            [three] => thyyree

            [four] => 445344

            [five] => 5345355

            [events] => Array

                (

                )


        )

)

我只需要获取事件并删除空事件。然而,首先我尝试获取像这样的事件,但它给出了一个错误。


                               $new = [];

                                 for($i=0 ; $i < count($data)-1 ; $i++)

                                {

                                  if(count($data[$i]['events']) > 0)

                                  {

                                    

                                    $new[] = $data[$i]['events'];

                                   

                                  }

                                  

                                }

                                print_r ($new);

但它给了我一个错误: Cannot use object of type Illuminate\Http\JsonResponse as array


谁能帮我解决这个问题吗?


慕容森
浏览 322回答 2
2回答

慕姐8265434

你需要解码$data[0]而不是$data因为你正在做$data[]&nbsp;=同样的 for 循环,你应该这样做if(count($data[0][$i]['events'])&nbsp;>&nbsp;0)

回首忆惘然

您可以像这样使用 Laravelcollect() 方法:collect($your_array)->toJson();https://laravel.com/docs/7.x/collections
随时随地看视频慕课网APP
我要回答