Array that is turning into object

Asked

Viewed 56 times

0

When using the json Decode function on a json, I’m having a problem. When the numbering is perfectly in order(0,1,2,3...) it is classified as arra:

[2016-08-01] => Array
    (
        [0] => 07:00:00
        [1] => 07:20:00
        [2] => 07:40:00
        [3] => 08:00:00
        [4] => 08:20:00
        [5] => 08:40:00
        [6] => 09:00:00
        [7] => 09:20:00
        [8] => 09:40:00
        [9] => 10:00:00
        [10] => 10:20:00
        [11] => 10:40:00
        [12] => 11:00:00
    )

However, when I need to remove some schedules it automatically turns into object (I removed the element of Indice 9 and 10)

[2016-07-25] => stdClass Object
        (
            [0] => 07:00:00
            [1] => 07:20:00
            [2] => 07:40:00
            [3] => 08:00:00
            [4] => 08:20:00
            [5] => 08:40:00
            [6] => 09:00:00
            [7] => 09:20:00
            [8] => 09:40:00
            [11] => 10:40:00
            [12] => 11:00:00
        )

I would like to know how to prevent this from happening, or if not, pass all my data to object, or all data to array.

  • How are you making that withdrawal?

  • 1

    you pass the second argument as true at all times?

  • 2

    Exact should put the second argument of json_decode as true if you want to transform into array instead of obj

  • I have a function that checks the available schedules of a professional. I make a series of checks to see if you have exceptions, filled out schedules, blockades. Then, when arriving at the end of the function, my initial array has undergone a lot of changes. Basically I took an item from that array with unset();

  • Perfect. The true parameter worked. Thank you.

  • So you’re doing a cast there on these validations.

  • Add the JSON format and how you are capturing and removing the data in the question, otherwise you cannot answer objectively. It’s a very simple problem, but you need this data to be consistent with the answers to the question.

Show 2 more comments

1 answer

2

Friend before using the object forces a cast to array with

$meu_array = (array)$obj;

So it will always be array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.