John Davidson

php - Extract name attribute from form field

0 comments
Message:


I have an array like this:


Array
(
[enabled] => Array
(
[:input[name="email"]] => Array
(
[checked] => 1
)

)

)

what would be the best way to extract the name attribute using php?
I need to check the value of email like this:


if(!empty($array['enabled']['email'])){
// do some stuff.
}

The name value is set by some external component and that can be anything and what i need to to is, always check on my implementation that this dynamic attribute is set and not empty. This form field is added by other module and i am trying to check this on hook_form_alter and add validation handler based on the value submitted.


Thanks in advance.



My Answer:
> [1] => Array
> (
> [name] => field1
> [type] => text
> [label] => Field 1
> [value] => Value 1
> )
> [2] => Array
> (
> [name] => field2
> [type] => text
> [label] => Field 2
> [value] => Value 2
> )
> [3] => Array
> (
> [name] => field3
> [type] => text
> [label] => Field 3
> [value] => Value 3
> )
> )

To extract the `name` attribute from each form field in the array, you can loop through the array and access the `name` key of each sub-array. Here's an example code snippet in PHP:

php
foreach ($array as $field) {
echo $field['name'] . "
";
}


This code will output the `name` attribute of each form field in the array:


field1
field2
field3

Rate this post

5 of 5 based on 3455 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us