Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从细枝数组创建细枝变量_Php_Codeigniter_Twig - Fatal编程技术网

Php 从细枝数组创建细枝变量

Php 从细枝数组创建细枝变量,php,codeigniter,twig,Php,Codeigniter,Twig,我将codeigniter与Twig一起使用——我试图创建一个动态选择框,其中包含来自数据库的数据 这就是我现在正在做的 // animals is $data['animals']=$this->loadmodel->some_function() // animals passed from the controller ... <select id="foo" class="form-control">

我将codeigniter与Twig一起使用——我试图创建一个动态选择框,其中包含来自数据库的数据

这就是我现在正在做的

      // animals is $data['animals']=$this->loadmodel->some_function()
      // animals passed from the controller ...


           <select  id="foo" class="form-control">
                <option selected="true" >Animals</option>
                   {%  for animal_key in animals %}
                      <option >
                     {{ animal_key ["animal_description"] }}
                     </option>
                   {% endfor %}
             </select>
设置控制器变量及其键的数组:

         {% set controller_names = ['animals','orders']%}
         {% set controller_vars = 
           ['animals'=>'animal_description','orders'=>'order_description']%}
然后像这样迭代

         {%  for names in controller_names  %}
              <select  id="foo" class="form-control">
                <option selected="true" >{{ name }}</option>
                   {%  for controller_key in controller_vars %}
                      <option >
                     {{ controller_vars [ controller_key] }} //suppose to be Twig variables
                     </option>
                   {% endfor %}
             </select>
         {% endfor %} 
{%用于控制器中的名称\u names%}
{{name}}
{controller_-vars%中的controller_-key的%
{{controller\u vars[controller\u key]}//假设是细枝变量
{%endfor%}
{%endfor%}
因此,我需要的是将set controller_vars数组转换为Twig变量(只要可能)…

您可以使用以下函数:

               {%  for controller_key in controller_vars %}
                  <option >
                    {{ attribute(controller_key, controller_vars) }}
                 </option>
               {% endfor %}
{%for controller\u key in controller\u vars%}
{{属性(控制器\键,控制器\变量)}
{%endfor%}
希望此帮助可以使用以下功能:

               {%  for controller_key in controller_vars %}
                  <option >
                    {{ attribute(controller_key, controller_vars) }}
                 </option>
               {% endfor %}
{%for controller\u key in controller\u vars%}
{{属性(控制器\键,控制器\变量)}
{%endfor%}

希望这有帮助

谢谢-但它不起作用…你能给我一个扩展的例子吗?嗨@RoyBarOn对不起,我在例子中犯了一个错误:我颠倒了属性函数的参数(我更新了我的答案)你能试试吗?对不起-不起作用-我试图“告诉”twig键和值都是php变量…hi@RoyBarOn可能我不理解你的问题,你能用一个复制吗?谢谢-但它不起作用…你能给我一个扩展的例子吗?hi@RoyBarOn抱歉,我在示例中犯了一个错误:我颠倒了属性函数的参数(我更新了答案)你能试一试吗?抱歉-不起作用-我试图“告诉”twig键和值是php变量…嗨@RoyBarOn可能我不明白你的问题,你能用一个复制吗?