help deleting an empty array from a multidimensional array
Posted: Sat May 30, 2009 10:40 pm
Hi, I'm fairly new to PHP, am in an advanced PHP class in college now and I'm working on a project that requires me to import a comma separated list of users (first name, last name, email address) into a mysql db but I'm having an issue with the import and would like some help.
I have the import function working, E.G it does import the users into the db, however, when my function gets to the end of the CSV file it adds an empty array and, of course, gives me a mysql error because of the null entries of the last array.
I'm trying to figure out how to select and erase empty arrays from the multidimentional array $lineItem2, and have been working on it all day, but the solution eludes me.
First, here is what the array looks like after its imported and processed:
(note the empty array at bottom)
Below is the code i've come up with for searching through the array to find empty arrays, but my method to delete the empty key isnt working. I'm sure its a quick fix for someone out there, but I'm sort of a PHP noob... still learning... and not very good with arrays
:
$lineItem2 is the multidimensional array that contains all the user info.
I'm sure there are a lot of other, more desirable/efficient ways to do this, but like I said I'm still new to PHP and still learning...
Any help would be greatly appreciated, thanks!
I have the import function working, E.G it does import the users into the db, however, when my function gets to the end of the CSV file it adds an empty array and, of course, gives me a mysql error because of the null entries of the last array.
I'm trying to figure out how to select and erase empty arrays from the multidimentional array $lineItem2, and have been working on it all day, but the solution eludes me.
First, here is what the array looks like after its imported and processed:
(note the empty array at bottom)
Code: Select all
Array(
[0] => Array(
[fname] => Todd
[lname] => Preston
[email] => tpreston@somewhere.com)
[1] => Array(
[fname] => Sam
[lname] => Phillips
[email] => sphillips@somewhere.net)
[2] => Array(
[fname] =>
[lname] =>
[email] => )
)$lineItem2 is the multidimensional array that contains all the user info.
Code: Select all
foreach($lineItem2 as $key => $value){
//fname is the first value in each array, if it's empty, drop it.
if($value['fname'] === ''){
unset($key); //not working
}
//set values for db insertion
$fname = $value['fname'];
$lname = $value['lname'];
$email = $value['email'];
//insert new array values into db
$userAdmin->import_users($fname,$lname,$email);
}I'm sure there are a lot of other, more desirable/efficient ways to do this, but like I said I'm still new to PHP and still learning...
Any help would be greatly appreciated, thanks!