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
CodePudding user response:
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"]);
CodePudding user response:
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]);
}
}
CodePudding user response:
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);
