php - Iterate through multiple arrays and check if all elements are non-empty (or strlen is at least 1) -
for example have 3 arrays. $_post
arrays looks
$_post['row_id']
- $_post['row_id'][0]
, $_post['row_id'][1]
, etc.
$_post['date_day']
- $_post['date_day'][0]
, $_post['date_day'][1]
, etc.
$_post['date_month']
- $_post['date_month'][0]
, $_post['date_month'][1]
, etc.
want iterate through $_post['row_id']
, if $_post['date_day']
, $_post['date_month']
non-empty create new array $if_non_empty
value 1.
data these:
$_post['row_id'][0]
1
$_post['row_id'][1]
2
$_post['date_day'][0]
1
$_post['date_day'][1]
blank/empty
$_post['date_month'][0]
4
$_post['date_month'][1]
4
created code
foreach ($_post['row_id'] $i => $row_id) { if ( (strlen($_post['date_day']) >= 1) , (strlen($_post['date_month']) >= 1) ) { $if_non_empty = 1; print_r ($if_non_empty); echo ' $if_non_empty<br>'; } }
so expect
code first time goes through foreach ($_post['row_id'] $i => $row_id) {
then first time goes through if ( (strlen($_post['date_day']) < 1) , (strlen($_post['date_month']) < 1) ) {
as $_post['date_day'][0]
, $_post['date_month'][0]
non-empty (values 1 , 4), creates array $if_non_empty[0] value 1.
but code displays nothing. tried empty , non-empty values.
this
print_r ($if_non_empty); echo ' $if_non_empty<br>';
displays nothing.
my final aim this
1) code above want create array $if_non_empty
. depending on $if_non_empty
can manage arrays non-empty values.
2) iterate through $if_non_empty
, insert arrays non-empty values mysql.
there multiple input rows multiple values each row.
some input fields in row may empty. such input rows not want insert.
such solution try create insert rows input fields non empty.
have @ php manual.
strlen return length of string on success, , 0 if string empty.
better use empty function http://php.net/manual/en/function.empty.php
Comments
Post a Comment