I've got an array that I've imported from a csv. But the last line on the csv is epmty, yet it still imports the empty values into the array.
我有一个我从csv导入的数组。但是csv的最后一行是epmty,但它仍然将空值导入到数组中。
Is there an easy way to check if an entire row is empty within an array?
有没有一种简单的方法来检查数组中整行是否为空?
Here's the code I'm using (I'm checking the array against the contents of another array to build a mySQL statement:
这是我正在使用的代码(我正在根据另一个数组的内容检查数组以构建一个mySQL语句:
foreach (array_keys($array[0]) as $arraykey => $dbtitles){
foreach ($fields as $dbkey => $dbfield){
if ($dbtitles == $dbfield && !empty($arraykey) ){
$insertSQL .= "student_" . $dbkey . ", ";
}
}
}
Apologies if I'm being stupid.
抱歉,如果我是愚蠢的。
Here's the last part of my array:
这是我的数组的最后一部分:
[21] => Array
(
[Username] =>
[Last_Name] => Jones
[First_Name] => Tom
[Email] =>
[Password] => password
[Student_Id] =>
[Middle_Name] =>
[Job_Title] =>
[Department] =>
[Company] =>
[Street_1] => 21 A Road
[Street_2] =>
[City] => Exeter
[County] =>
[Postcode] => EX1 1AA
[Country] =>
[Work_Phone] =>
[Home_Phone] =>
[Work_Fax] =>
[Mobile_Phone] => 07111 222333
[Website] =>
[Role] => Exeter City
)
[22] => Array
(
[Username] =>
[Last_Name] =>
[First_Name] =>
[Email] =>
[Password] =>
[Student_Id] =>
[Middle_Name] =>
[Job_Title] =>
[Department] =>
[Company] =>
[Street_1] =>
[Street_2] =>
[City] =>
[County] =>
[Postcode] =>
[Country] =>
[Work_Phone] =>
[Home_Phone] =>
[Work_Fax] =>
[Mobile_Phone] =>
[Website] =>
[Role] =>
)
3 个解决方案
#1
2
Like what's said in the duplicate, take a look at array_filter
就像重复中所说的那样,看一下array_filter
If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.
如果没有提供回调,则将删除所有输入等于FALSE的条目(请参阅转换为布尔值)。
So can do this in one simple line.
所以可以在一个简单的行中做到这一点。
if(!array_filter($array)) {
// Every fields are empty, null or equal to false
}
#2
0
foreach (array_keys($array[0]) as $arraykey => $dbtitles){
foreach ($fields as $dbkey => $dbfield){
if ($dbtitles == $dbfield && !empty($arraykey) ){
$insertSQL .= "student_" . $dbkey . ", ";
}
}
}
In your foreach you are only finding array keys for array [0], try doing it with just $array instead of $array[0]
在你的foreach中你只找到数组[0]的数组键,尝试用$ array代替$ array [0]
#3
0
You should try array_pop for better solution.
您应该尝试使用array_pop来获得更好的解决方案。
<?php
$CSVFileLines = array_pop(file('/path/to/your/data.csv'));
foreach($CSVFileLines as $CSVLine){
$CSVLineValues = array_map('mysql_real_escape_string', explode(',', $CSVLine));
$InsertSQL = sprintf("insert into tablename values ('%s')", implode("','", $CSVLineValues));
mysql_query($InsertSQL); #Depreated in All recent PHP Versions so you should use MySQLi
}
echo sprintf('There were %s lines contained in the targeted CSV.', count($CSVFileLines));
?>
#1
2
Like what's said in the duplicate, take a look at array_filter
就像重复中所说的那样,看一下array_filter
If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.
如果没有提供回调,则将删除所有输入等于FALSE的条目(请参阅转换为布尔值)。
So can do this in one simple line.
所以可以在一个简单的行中做到这一点。
if(!array_filter($array)) {
// Every fields are empty, null or equal to false
}
#2
0
foreach (array_keys($array[0]) as $arraykey => $dbtitles){
foreach ($fields as $dbkey => $dbfield){
if ($dbtitles == $dbfield && !empty($arraykey) ){
$insertSQL .= "student_" . $dbkey . ", ";
}
}
}
In your foreach you are only finding array keys for array [0], try doing it with just $array instead of $array[0]
在你的foreach中你只找到数组[0]的数组键,尝试用$ array代替$ array [0]
#3
0
You should try array_pop for better solution.
您应该尝试使用array_pop来获得更好的解决方案。
<?php
$CSVFileLines = array_pop(file('/path/to/your/data.csv'));
foreach($CSVFileLines as $CSVLine){
$CSVLineValues = array_map('mysql_real_escape_string', explode(',', $CSVLine));
$InsertSQL = sprintf("insert into tablename values ('%s')", implode("','", $CSVLineValues));
mysql_query($InsertSQL); #Depreated in All recent PHP Versions so you should use MySQLi
}
echo sprintf('There were %s lines contained in the targeted CSV.', count($CSVFileLines));
?>