StuBlackett StuBlackett - 1 year ago 76
PHP Question

PHP Array - Get the key value

Possible Duplicate:

PHP get both array value and array key

I am using Codeigniters' form_checkbox() method.

Using a foreach loop I am creating the form_checkbox and the forms' label. This is all fine, But I need to get the value from the array.

My array setup is as follows :

[1] => Animals
[2] => Art and Culture
[3] => Children
[4] => Disability
[5] => Disaster Relief
[6] => Domestic Violence

My PHP code is as follows :

<?php foreach($interests as $interest)
echo form_checkbox('user_interests[]', $interest);
echo "<label>$interest</label>";

This produces HTML Like :

<input type="checkbox" value="Animals" name="user_interests[]">

What I would like it to be is the value = "1", "2" etc from the Array key.

How do I get this?

Answer Source

try this :

<?php foreach($interests as $k=> $interest)
           $data= array('name'=>'user_interests[]', 'value'= $k)
            echo form_checkbox($data);
            echo "<label>$interest</label>";