如何获取数组值

时间:2015-11-09 06:46:39

标签: php codeigniter

我想在视图文件的组合框中显示实用程序库中的数组值。 这是我的实用程序库代码

    class Utility
     {
   public $email_hooks = array(
    "create_user" => array(
            "name" => "New user creation",
            "keys" => array(
                "site_url" => "Site URL",
                "current_date" => "Curremt Date",
                "user_name" => "User name", 
                "name" => "Name of the user",
                "password" => "Password of user",
                "user_email"  => "Email address of new user"
            )               
        ),
    "register_user" => array(
            "name" => "New user registration",
            "keys" => array(
                "site_url" => "Site URL",
                "current_date" => "Curremt Date",
                "user_name" => "User name", 
                "name" => "Name of the user",
                "activation_url" => "Url for activation registered account",
                "user_email"  => "Email address of the user"
            )
        ),
    "forgot_password" => array(
            "name" => "Forgot Password",
            "keys" => array(
                "site_url" => "Site URL",
                "current_date" => "Curremt Date",
                "user_name" => "User name",
                "name" => "Name of the user",
                "reset_url" => "Url for resetting password",
                "user_email"  => "Email address of the user"
            )
        )
     );
   }

在视图部分中,我想在组合框中仅显示“新用户创建”,“新用户注册”和“忘记密码”的值。我在视图文件中使用了以下代码。

     <select id="choose">
        <option>-- Select One --</option>
        <?php
        foreach ($email_hooks as $key => $value)
        {
          foreach ($value as $value => $key)
          {
          echo '<option value="'.$key.'">'.$key.'</option>';
          }
        }
        ?>
        </select>
        <div class="col-lg-4" id="email_tmpl"></div>

然后输出为“新用户创建”,“数组”,“新用户注册”,“数组”,“忘记密码”,“数组”。

我还有一个问题。当我选择组合框值时,我想显示一个包含

等记录的div

%site_url% - 网站网址 %current_date% - Curremt Date %user_name% - 用户名 %name% - 用户的名称 %password% - 用户密码 %user_email% - 新用户的电子邮件地址。

我在js part

中使用了以下代码
  $(document).ready(function () 
 {
 $('#choose').change(function(event) {
    $.post('info', { selected: $('#choose').val() },
        function(data) {
            $('#email_tmpl').html(data);
        }
    );            
   }); 

  });

这是控制器代码

    public function info()
{
$this->load->library('Utility');
$email_hooks = $this->utility->email_hooks;
$selected = isset($_POST['selected']) ? $_POST['selected'] : 'nothing';
//echo("This is $selected and other info");
foreach($email_hooks as $key=>$val)
{


    foreach($val['keys'] as $keys=>$value)
    {
        echo '%'.$keys.'%<br>';
    }
}

}

现在输出

%SITE_URL%
%CURRENT_DATE%
%USER_NAME%
%名称%
%密码%
%USER_EMAIL%
%SITE_URL%
%CURRENT_DATE%
%USER_NAME%
%名称%
%activation_url%
%USER_EMAIL%
%SITE_URL%
%CURRENT_DATE%
%USER_NAME%
%名称%
%reset_url%
%user_email%

我想要输出 %SITE_URL%
%CURRENT_DATE%
%USER_NAME%
%名称%
%密码%
%user_email% - 用于新用户创建

%SITE_URL%
%CURRENT_DATE%
%USER_NAME%
%名称%
%activation_url%
%user_email% - 用于新用户注册。

%SITE_URL%
%CURRENT_DATE%
%USER_NAME%
%名称%
%reset_url%
%user_email% - Forgot Password

2 个答案:

答案 0 :(得分:3)

试试这个:

foreach($email_hooks as $key=>$val){
    echo $val['name'];
}

您的选择框:

<select>
<option>-- Select One --</option>
 <?php 
foreach($email_hooks as $key=>$val){
    echo '<option value="'.$key.'">'.$val['name'].'</option>';
}
?>
 </select>

答案 1 :(得分:0)

看看你可以在下拉列表中获得所有价值

$utility = new Utility();
   //print_r($utility->email_hooks);
   echo "<select>
     <option>-- Select One --</option>";

   foreach($utility->email_hooks as $key => $val){
      echo '<option value="">'.$val["name"].'</option>';
       foreach($val as $k=>$v){
          //    echo '<option value="">'.$v.'</option>';
            if(is_array($v)){
            foreach($v as $k1 => $v1){
                echo '<option value="">'.$v1.'</option>';
            }
            }

       }

   }

   echo "</select>";