从PHP中的数组动态创建PHP代码


dynamic creation of php code from array in php

我有一个代码生成php代码从一个数组的词,但词的列表是可变的如:

    $list=array(
    "BMW",
    "MUSTANG",
    "DBM",
    "Txt62"
    );
$arrlength=count($list);
for($x=0;$x<$arrlength;$x++)
  {
  echo ' ''' .$list[$x]. '''' . ' => $this->input->post("'.$list[$x].'") == '''' ? ''Not defined definido'' : $this->input->post("'.$list[$x].'"),  ';
  echo "<br>";
  }

是否有更好的方法来做到这一点,像一个函数,我传递的词数组,它返回php代码?这是可能的php代码?

要理解您要做的事情并不容易,但是如果要对数组中的每个项运行函数,则可以使用array_walk ?

$list = array(
              "BMW",
              "MUSTANG",
              "DBM",
              "Txt62"
             );
function validate($item, $key) {
    echo ' ''' .$item. '''' . ' => $this->input->post("'.$item.'") == '''' ? ''Not defined definido'' : $this->input->post("'.$item.'"),  <br>';
}
array_walk($list, 'validate');