0

i have data of set like this:

$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
                (
                )

        )
)

I need to fetch the event only and remove the empty events. However first i tried to fetch the events liket this, But it give an error.

                               $new = [];
                                 for($i=0 ; $i < count($data)-1 ; $i++)
                                {
                                  if(count($data[$i]['events']) > 0)
                                  {
                                    
                                    $new[] = $data[$i]['events'];
                                   
                                  }
                                  
                                }
                                print_r ($new);

But it give me an error : Cannot use object of type Illuminate\Http\JsonResponse as array

Can anyone please help me to resovle the issue?

Thank you

7
  • json_decode($data) First u need to decode json first. After that u can access their value in php way. Commented Jun 26, 2020 at 6:54
  • Hi, i tried to decode the json, it give me message":"json_decode() expects parameter 1 to be string, array given" ...I also tried to convert from string to array like this $data = array($data); but not working Commented Jun 26, 2020 at 7:00
  • Please provide the Full Error Message with Line Number. Also specify the line in your question. Commented Jun 26, 2020 at 7:04
  • try like this $data[$i]->events Commented Jun 26, 2020 at 7:06
  • I tried $data[$i]->events and it igive me error like : Trying to get property of non-object","status_code": Commented Jun 26, 2020 at 7:10

2 Answers 2

2

You need to decode $data[0] not $data since you're doing

$data[] =

same for loop ,you should do

if(count($data[0][$i]['events']) > 0)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use Laravel collect() method like this:

collect($your_array)->toJson();

https://laravel.com/docs/7.x/collections

1 Comment

Hi, I have tried to convert to Json using collect, however, i received a blank Array. Do you know why this happened?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.