Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
How to remove last array from below code when name is empty

Here I want to remove last array from below (this is always last array that is set dynamically):

array(1) {
  ["file-1573204001988"]=>
  array(3) {
    [0]=>
    array(5) {
      ["name"]=>
      string(7) "abc.png"
      ["type"]=>
      string(9) "image/png"
      ["tmp_name"]=>
      string(14) "/tmp/phpdl81UY"
      ["error"]=>
      int(0)
      ["size"]=>
      int(72761)
    }
    [1]=>
    array(5) {
      ["name"]=>
      string(12) "profiles.png"
      ["type"]=>
      string(9) "image/png"
      ["tmp_name"]=>
      string(14) "/tmp/phptIFS1J"
      ["error"]=>
      int(0)
      ["size"]=>
      int(16178)
    }
    [2]=>
    array(5) {
      ["name"]=>
      string(0) ""
      ["type"]=>
      string(0) ""
      ["tmp_name"]=>
      string(0) ""
      ["error"]=>
      int(4)
      ["size"]=>
      int(0)
    }
  }
}

I want to remove whole below array from above (when it have empty name):

[2]=>
    array(5) {
      ["name"]=>
      string(0) ""
      ["type"]=>
      string(0) ""
      ["tmp_name"]=>
      string(0) ""
      ["error"]=>
      int(4)
      ["size"]=>
      int(0)
    }

How this can possible I tried with array_pop, unset and other predefined php functions but not getting the correct result. Please help me out to reach the correct value Thanks in advance

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

If you are persistently trying to remove the last element within the array, you can use array_pop.

array_pop() pops and returns the value of the last element of array, shortening the array by one element.

$myArray = array(
    0 => "hello",
    1 => "world",
    2 => "remove me",
);

array_pop($myArray); // Removes [2] from the array above.

In your case, you would simply do:

array_pop($array["file-1573204001988"]);
over 4 years ago · Santiago Trujillo Relatório

0

A simple foreach loop with a simple test in it will do this for you

foreach ($arr['file-1573204001988'] as $key=>$a){
    if ( $a['name'] = '' ){
        unset($arr['file-1573204001988'][$key]);
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

These look like items of $_FILES array. Instead of checking the name I would recommend checking the value of error and filter out anything that's not UPLOAD_ERR_OK or 0

$valid_files = array_filter($arr["file-1573204001988"],
     fn($item) => $item['error'] === UPLOAD_ERR_OK);

File upload errors

array_filter

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda