如何在PHP中的不同变量中存储数组元素 [英] How to store array elemnts in diffrent variables in php

查看:48
本文介绍了如何在PHP中的不同变量中存储数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择的语言下拉列表,我很想把数组的每个元素都放在diffrent变量中.为了让您理解我的问题,我将在此处输入代码:

I have a select drop down list of languages and I'm intrested of putting each element of the array in diffrent variable. For you understanding my question I will put here a code:

<html dir="ltl">
<?php
header('Content-Type: text/html; charset=utf-8');
$db=mysqli_connect("localhost","root","","travelersdb");
mysqli_query($db,"SET NAMES 'utf8'");
?>

    <head>
            <link href="Styles/StyleEx.css" rel="stylesheet" type="text/css"/>
    </head>

    <script type="text/javascript">

         var expanded = false;

        function showCheckboxes() {
          var checkboxes = document.getElementById("checkboxes");
          if (!expanded) {
            checkboxes.style.display = "block";
            expanded = true;
          } else {
            checkboxes.style.display = "none";
            expanded = false;
          }
        }   
        </script> 
<body>


<tr>
      <td>Languages</td>
      <td dir="rtl">

          <form method="post" action="exxx.php">
          <div class="multiselect" dir="rtl">
          <div class="selectBox" onclick="showCheckboxes()" dir="rtl">
        <select>
        <option>Choose language:</option>
        </select>
        <div class="overSelect" dir="rtl"></div>
        </div>
        <div id="checkboxes">
        <label for="one">  
            <input type="checkbox"  id="one" name="languages[]" value="English">English</label>
            <label for="two">
            <input type="checkbox" id="two" name="languages[]" value="German" >German</label>
            <label for="three">
            <input type="checkbox" id="three" name="languages[]" value="French">French</label>
              <label for="four">
            <input type="checkbox" id="four" name="languages[]" value="Spanish">Spanish</label>
              <label for="five">
            <input type="checkbox" id="five" name="languages[]" value="Italien">Italien</label>
      </div>
  </div>  
              <input type="submit" name="submit">
          </form>

      </td>
      </tr>        

<?php

  if (isset($_POST['submit'])) {
 $languages = $_POST['languages'];


    $language1 =  $languages[0];
    $language2 =  $languages[1];
    $language3 =  $languages[2];
    $language4 =  $languages[3];
    $language5 =  $languages[4];

    echo $language1;
    echo $language2;
    echo $language3;
    echo $language4;
    echo $language5;
  }
?>

</body>
</html>

如果我选中所有复选框,则没有问题,但是如果我选中的复选框少于5个,我将收到此消息:

If I'm checking all the checkboxs there isn't a problem but if I'm checking less then 5 I will get this message:

Notice: Undefined offset: 

这是因为数组大小.如果检查的语言少于5种,如何避免这种情况并回显变量?

This is beacuse of the array size. How can I avoid this and echo the variables if there were less than 5 languages checked?

推荐答案

您可以使用$$定义变量.查看 实时演示

You can use $$ to define a variable. check the live demo

请注意,您可以访问foreach范围之外的所有变量.

foreach($languages as $k => $v)
{
  $name = 'language' . ($k + 1);
  $$name = $v;
}

print_r($language1);

这篇关于如何在PHP中的不同变量中存储数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆