0

I have the next json:

[
    {
        "clave": "es",
        "content": {
            "barra": [
                "a",
                "a",
                "N",
                "C"
            ],
            "letras_inicio": "a",
            "titulos_servicios": [
                "A",
                "A",
                "A"
            ],
            "desc_servicios": [
                "O",
                "O",
                "E"
            ],
            "titulo_nosotros": "n",
            "descripcion_nosotros": "S",
            "titulos_especialidades": [
                "A",
                "A"
            ],
            "desc_especialidades": [
                "E",
                "L"
            ],
            "titulo_frm_contacto": "Co",
            "frmContacto": [
                "N",
                "c",
                "C",
                "A",
                "M",
                "E"
            ]
        }
    },
    {
        "clave": "por",
        "content": {
            "barra": [
                "f",
                "f",
                "f",
                "f"
            ],
            "letras_inicio": "f",
            "titulos_servicios": [
                "f",
                "f",
                "f"
            ],
            "desc_servicios": [
                "qf",
                "qf",
                "qf"
            ],
            "titulo_nosotros": "f",
            "descripcion_nosotros": "qf",
            "titulos_especialidades": [
                "f",
                "f"
            ],
            "desc_especialidades": [
                "qf",
                "qf"
            ],
            "titulo_frm_contacto": "f",
            "frmContacto": [
                "f",
                "f",
                "f",
                "f",
                "f",
                "f"
            ]
        }
    }
]

The next code is that i use to delete a node from json file:

$json = file_get_contents('php://input');
$obj = json_decode($json);
$index = filter_var($obj->index);
$data = file_get_contents('languajes.json');
$data = json_decode($data,true);
unset($data[$index]);
if($data = json_encode($data,JSON_PRETTY_PRINT)){
    echo 'success';
}else{
    echo 'failed';
}
file_put_contents('languajes.json', $data);

So, when I save the json it looks like:

{
    "0": {
        "clave": "es",
        "content": {
            "barra": [
                "I",
                "S",
                "N",
                "C"
            ],
            "letras_inicio": "N",
            "titulos_servicios": [
                "A",
                "A",
                "A"
            ],
            "desc_servicios": [
                "O",
                "O",
                "E"
            ],
            "titulo_nosotros": "N",
            "descripcion_nosotros": "S",
            "titulos_especialidades": [
                "A",
                "A"
            ],
            "desc_especialidades": [
                "E",
                "L"
            ],
            "titulo_frm_contacto": "C",
            "frmContacto": [
                "N",
                "A",
                "C",
                "A",
                "M",
                "E"
            ]
        }
    },
    "2": {
        "clave": "en",
        "content": {
            "barra": [
                "H",
                "S",
                "A",
                "C"
            ],
            "letras_inicio": "W",
            "titulos_servicios": [
                "W",
                "M",
                "D"
            ],
            "desc_servicios": [
                "W",
                "W",
                "W"
            ],
            "titulo_nosotros": "A",
            "descripcion_nosotros": "W",
            "titulos_especialidades": [
                "W",
                "m"
            ],
            "desc_especialidades": [
                "f",
                "a"
            ],
            "titulo_frm_contacto": "C",
            "frmContacto": [
                "F",
                "L",
                "E",
                "S",
                "M",
                "S"
            ]
        }
    }
}

The problem is when y try to read the data after save the data in json file, the number that php put it causes that I can't read the file. how can I avoid that this happen?

I will be to grateful for your answers

2
  • What data are you passing from the AJAX Call Commented Apr 1, 2020 at 21:33
  • 1
    reset the index, file_put_contents('languajes.json', array_values($data)); Commented Apr 1, 2020 at 21:33

1 Answer 1

2

unset() just removes an index from an array, it doesn't adjust the indexes of all the other elements. When an array's indexes aren't sequential numbers starting at 0, json_encode() encodes it as an object, not an array.

After unsetting, you can reset all the indexes with

$data = array_values($data);

Or you can use array_splice() to remove the element:

array_splice($data, $index, 1);
Sign up to request clarification or add additional context in comments.

Comments

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.