user773961
user773961

Reputation: 141

Multidimensional array in foreach

i want to do the foreach only if the content of myformdata[languages1][] is not empty (include zero)

I already try:

  foreach((!empty($form['languages1']) as $val){

and

if (!empty($form['languages1'][])) {
foreach($form['languages1'] as $val){
//do stuff
}

i don't have any success. At the moment with the code below the loop is made when the input of myformdata[languages1][] is 0

foreach

foreach($form['languages1'] as $val){
//do stuff
}

thanks

Upvotes: 1

Views: 126

Answers (2)

timw4mail
timw4mail

Reputation: 1766

You're dealing with type coersion

You probably want something like

if(!empty($form['languages1']) && $form['languages1'] !== 0)

So that PHP will match 0 as a number, and not as false.

Upvotes: 1

Francois Deschenes
Francois Deschenes

Reputation: 24969

foreach ( $form['languages1'] as $val )
{
  // If the value is empty, skip to the next.
  if ( empty($val) )
    continue;
}

Reference: http://ca.php.net/continue

Upvotes: 2

Related Questions