Jump to content

loops and multicolumn arrays


garry27

Recommended Posts

I've been trying to create  a loop for the following array but I keep getting errors.

 

   $formOutputData[] = array(array('name'=>'username',
                                   'longName'=>'Username',
                                   'maxLen'=>20,
							   'isRequired'=>true
							   ),
                             array('name'=>'forename',
						       'longName'=>'Forename', 
						       'maxLen'=>15,
							   'isRequired'=>true
							   ),
                             array('name'=>'surname',
						       'longName'=>'Surname',
						       'maxLen'=>20,
							   'isRequired'=>true
							   ),
                             array('name'=>'town',
						       'longName'=>'Town',
						       'maxLen'=>20,
							   'isRequired'=>false
							   ),
                             array('name'=>'userEmail',
						       'longName'=>'Email Address',
						       'maxLen'=>40,
							   'isRequired'=>true
							   ),
                             array('name'=>'userPassword',
						       'longName'=>'Password',
						       'maxLen'=>20,
							   'isRequired'=>true
							   ),
                             array('name'=>'userPassword2',
						       'longName'=>'Password',
						       'maxLen'=>10000,
							   'isRequired'=>false
							   )
                             ); 

 

 

here is the loop I'm using (pretty much out of the book i'm using) but I get:

Warning: Variable passed to each() is not an array or object in /home/gigaddic/public_html/classes/user_input.php on line 96

 

  function getNameValues($formOutputData) //get expected key and value params
  {
    $expected = array();
    $num = count($formOutputData);

    for ( $row=0; $row < 6; $row++ ) {
  while (list($key,$value) = each($formOutputData[$row])) {
        if($value == 'name' && $_POST[$value] ) {
          $expected[$key] = $value ;
     }
  }
}
return $expected;
  }

 

Please advise me

Link to comment
https://forums.phpfreaks.com/topic/123332-loops-and-multicolumn-arrays/
Share on other sites

Right... here is your problem:

 

when you define your array you put:

 

$formOutputData[] = array(array(... blah blah blah

 

this should be :

 

$formOutputData = array(array(... blah blah blah

 

so...

 

$stuff[] = 'some stuff';

 

..will create a valid single dimension array:

 

$stuff[] = array(0=>"some other stuff");

 

Creates an array within an array.

 

You have actually created an array thats 3 levels deep but your loop is designed to handle an array thats 2 levels deep.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.