Foreach help

Hi, I have this array code:

$formData = array (
“fieldName” => array(“field_1” => “name”, “field_2” => “address1”, “field_3” => “address2”, “field_4” => “postcode”, “field_5” => “email”),
“fieldType” => array(“type_1” => “text”, “type_2” => “text”, “type_3” => “text”, “type_4” => “text”, “type_5” => “text”),

);

Then I have:

foreach ($formData as $k => $v) {

    echo "$k\

";

}

which outputs:
fieldName
fieldType

I’m very new to arrays and wondered how the foreach needs to be to echo the actual data, not the array names?

Thanks,

zaas.

Ok do in this way then:


$fields = array (
    array('fieldname'=>'name', 'fieldtype'=>'text'),
    array('fieldname'=>'address1', 'fieldtype'=>'text'),
    array('fieldname'=>'address2', 'fieldtype'=>'text'),
    array('fieldname'=>'postcode', 'fieldtype'=>'text'),
    array('fieldname'=>'email', 'fieldtype'=>'text')
);
foreach($fields as $field){
    echo '<input name="' . $field['fieldname'] . '" type="' . $field['fieldtype'] . '" />' . "<br />";
}

Actually I could well be going about this the wrong way. What I want to do is build up some text fields from the array.

Such as:
<input name=“name” type=“text” />
<input name=“address1” type=“text” />

The foreach loop only gives me one value at time where I need one value from fieldName and one value from the fieldType at the same time to rebuild the form fields.

Any ideas what might be the best function to do this?

Thanks,

Zaas

Ahh, perfect. Thanks Raju. Ultimately I want to create a CRUD system but just feeling my way forward at the moment! Zaas

Your array is multi dimensional so you need to loop through the inside array was well:


$formData = array (
    "fieldName" => array("field_1" => "name", "field_2" => "address1", "field_3" => "address2", "field_4" => "postcode", "field_5" => "email"),
    "fieldType" => array("type_1" => "text", "type_2" => "text", "type_3" => "text", "type_4" => "text", "type_5" => "text")
);
foreach($formData as $k=>$v){
    echo "<strong>$k</strong>" . '<br />';
    foreach($v as $k1=>$v1){
        echo "$v1<br />";
    }
}

Thanks Rajug, that’s make much more sense now.