You have two solutions depending on whether you can / want to improve or not the structure of the JSON data sent:
Maintaining the JSON format
You can iterate through each element using a foreach
and check that the index is not nombre
:
<?php
/* Reproducimos la variable $input: */
$input = json_decode('
{
"id": 1,
"data": {
"nombre": "Nombre",
"file1": "Mhuc2dvCg==",
"file2": "Yhj4dysYn==",
"file3": "Jusj6Hism==",
"file4": "Rhsy7Yhah=="
}
}
', true);
/* Simulamos tu función agregando información de depuración */
function subirFichero($base64) {
echo '<p>BASE64 recibido: ', $base64, '</p>', PHP_EOL;
}
/* Enumeramos los elementos e ignoramos el que se llame "nombre" */
foreach ($input['data'] as $clave => $base64) {
if ($clave !== 'nombre') {
subirFichero($base64);
}
}
The output obtained is:
BASE64 recibido: Mhuc2dvCg==
BASE64 recibido: Yhj4dysYn==
BASE64 recibido: Jusj6Hism==
BASE64 recibido: Rhsy7Yhah==
Optimizing the JSON format
Normally, the content of the JSON facilitates its analysis by the application that receives the data, therefore the ideal is to adjust its content to a property that enumerates the files unequivocally:
{
"id": 1,
"data": {
"nombre": "Nombre",
"files": [
"Mhuc2dvCg==",
"Yhj4dysYn==",
"Jusj6Hism==",
"Rhsy7Yhah=="
]
}
}
So the PHP code is greatly simplified:
<?php
$input = json_decode('
{
"id": 1,
"data": {
"nombre": "Nombre",
"files": [
"Mhuc2dvCg==",
"Yhj4dysYn==",
"Jusj6Hism==",
"Rhsy7Yhah=="
]
}
}
', true);
/* Simulamos tu función agregando información de depuración */
function subirFichero($base64) {
echo '<p>BASE64 recibido: ', $base64, '</p>', PHP_EOL;
}
/* Enumeramos cada uno de los elementos */
foreach ($input['data']['files'] as $base64) {
subirFichero($base64);
}
The output obtained is (as before):
BASE64 recibido: Mhuc2dvCg==
BASE64 recibido: Yhj4dysYn==
BASE64 recibido: Jusj6Hism==
BASE64 recibido: Rhsy7Yhah==