D
D
den4eg112018-10-30 20:29:07
PHP
den4eg11, 2018-10-30 20:29:07

How to correctly change json arrays so that indexing is not added?

I have a json like this:

,{
  "id": 12345678,
  "color": "red",
  "model": {
    "id": 1,
    "t": "321"
  }
},

,{
  "id": 323,
  "color": "green",
  "model": {
    "id": 2,
    "t": "654"
  }
},

I want to remove, for example, all elements where color = green
I use json decode and save the data into an array $result = array(): I
use iteration and unset($result[$i]);
at the output after json encode I get the correct result, but indexing appears:
"4":{
  "id": 12345678,
  "color": "red",
  "model": {
    "id": 1,
    "t": "321"
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2018-10-30
@den4eg11

At the end do
sort($result)

S
Stalker_RED, 2018-10-30
@Stalker_RED

array_values()

$json = json_encode(array_values($data), JSON_UNESCAPED_UNICODE);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question