Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!