H
H
hobu4ok912016-09-04 18:16:35
PHP
hobu4ok91, 2016-09-04 18:16:35

How to save data in php to json file, as an array of objects, without keys?

For example, there is a regular array like this:

$arr = [
    [
        'name' => 'test',
        'type' => 'test'
    ],
    [
        'name' => 'test',
        'type' => 'test'
    ],
    [
        'name' => 'test',
        'type' => 'test'
    ],
];

How to save such an array to a file in which the data would be in this form:
[
    {
        'name':'test',
        'type':'test'
    },
    {
        'name':'test',
        'type':'test'
    }
]

When saving using json_encode, objects are assigned keys, example
{
    '0':{
        'name':'test',
        'type':'test'
    },
   '1': {
        'name':'test',
        'type':'test'
    }
}

They are absolutely not needed, can anyone know how to get rid of them? I just met more than one project in which many json directories are connected, which contain thousands of records (that is, these keys cannot be manually deleted in any way), and all the data in them was without keys.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Bannik, 2016-09-04
@prodimon

By default, that's how it works.
af507fcd8f224449b832c28113414d94.png

A
Alexander Zolotykh, 2016-09-04
@zolotykh

json_encode converts the object "as is".
Most likely the array has keys.
Try to create a new array in a loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question