0

I have this code:

<?php
$adm['web'] = "www.tuweb.com";
$adm['titulo'] = "Mi título web";
$campos = array('adm[web]','adm[titulo]');
for ($i=0; $i<count($campos); $i++) {
?>
    /* add values in each field in the  value="" */
    <input type="text" name="<?php echo $campos[$i]; ?>" value="<?php echo ${$campos[$i]}; >"/>
<?php } ?>

The problem i have it´s when generate each field , in the value for each field no works value="" for add the value , only this problem

1
  • 1
    Question is not clear , what the aim of adding the known dimension values in to the $campos Array ? why not using it directly ? Commented Mar 1, 2012 at 4:47

4 Answers 4

2

${$campos[$i]} evaluates to ${adm[web]}. You don't have a variable named "adm[web]". You have a variable named "adm", which has a key named "web". The variable variable interpolation cannot resolve this though.

The solution is to use $campos = array($adm['web'], $adm['titulo']) instead of the complicated workaround you're trying.

Sign up to request clarification or add additional context in comments.

Comments

1

ONLY IF YOU to name the textfield as the array variable name and it value to be the value then this may be what you want

<?php
$adm['web'] = "www.tuweb.com";
$adm['titulo'] = "Mi título web";
$campos = array('adm[web]'=>$adm['web'], 'adm[titulo]'=>$adm['titulo']);
foreach($campos as $key=>$value){
?>
    <input type="text" name="<?php echo $key; ?>" value="<?php echo $value; ?>" />
<?php } ?>

this will set adm[web] and adm[titulo] textfield names {sounds fishy}

better approach would be to

<?php
$adm['web'] = "www.tuweb.com";
$adm['titulo'] = "Mi título web";
$campos = $adm;
foreach($campos as $key=>$value){
?>
    <input type="text" name="<?php echo $key; ?>" value="<?php echo $value; ?>" />
<?php } ?>

it will set the field names as the array keys i.e. 'web' and 'titulo'

hope this helps your case

Comments

0

Dirty though, but you could do:


echo eval('return $'. $campos[$i] . ';');

So it would be:


<input type="text" name="<?php echo $campos[$i]; ?>" value="<?php echo eval('return $'. $campos[$i] . ';');?>" />

Comments

0
<?php
    echo eval('return $'.preg_replace('~(?<=\[)(.*?)(?=\])~',"'$1'",$campos[$i]).';')
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.