Reputation: 563
This will be a quick one for most of you I'm sure, but I'm banging my head against the wall trying to teach myself multidimensional arrays.
I simply wish to check whether or not $_FILES["Photo"]["name"]
contains empty strings, and such the code below my if statement is executed.
Currently this works, as does my else statment (not shown), however there has to be a cleaner way of writing this??
Many thanks.
if (empty($_FILES["Photo"]["name"][0]) && empty($_FILES["Photo"]["name"][1]) && empty($_FILES["Photo"]["name"][2])) {
$query = "INSERT INTO lot (lotnumber, lottitle, lotdescription, datecreated, lastmodified) VALUES" .
"('$lotnumber', '$lottitle', '$lotdescription', NULL, NULL)";
if (!$mysqli->query($query)) {
echo '<p class="warning">Error executing INSERT query: (' . $mysqli->errno . ') ' . $mysqli->error . "</p>";
}
else
{
echo '<p class="success">The lot has been added to the directory.' . "</p>" . HTML_LINEBREAK;
}
}
Upvotes: 0
Views: 77
Reputation: 71384
You could use an array_filter()
, but I don't really see the problem with what you are doing:
$test_array = array_filter($_FILES['Photo']['name'], function($var) {
return empty($var);
});
if (count($test_array) === 3) {
$query = ... // the rest of your code
}
Of course this assumes that there are only three elements in the array. If you only want to check the first 3 elements, you would want to add an array_slice()
like this:
$test_array = array_filter(array_slice($_FILES['Photo']['name'], 0, 3), function($var) {
return empty($var);
});
Upvotes: 2
Reputation: 109
I would have a script that counts the keys within the "name" level.
$count = sizeof($name_of_array->Photo->name);
Then in a for loop check to see if the keys are empty.
for($i = 0; $i <= $count; $i++)
{
if(empty($name_of_array->Photo->name[$i])
{
... continue code
}
else
{
... continue code
}
}
Upvotes: 1