0

I'am having an issue which i cannot understand it , for example i have the following lines:

$form['field']['und'][0]['#type'] = 'select';
$form['field']['und'][1]['#type'] = 'select';
$form['field']['und'][2]['#type'] = 'select';
$form['field']['und'][3]['#type'] = 'select';

it works as expected , but if i try to change this hard coded method to :

foreach ($form['field']['und'] as $field)
            $field['#type'] = 'select';

it is not working anymore as expected...After a call like

 print render($form['field']);

for the first paragraph the result is good , as expected. But for the second paragraph it is not working anymore, the result is like nothing has been modified in the array. In the dsm($form) result either way the value of '#type' key is modified... I cannot understand the behavior. Please help!

2

Like PHP documentation says: In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>

http://php.net/manual/en/control-structures.foreach.php

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.