从方法转换变量到数组的堆栈

时间:2015-12-02 03:53:00

标签: php arrays variables methods

如何解决这个问题?

我的期望结果是点击按钮详细信息后的结果 请帮帮我..

<?php 
    $_SESSION['array1'] = array("zero","one","two");
    print_r($_SESSION['array1']);
?>
<form action="" method="post">
<input type="hidden" value="<?php print_r($_SESSION['array1']); ?>" name="detailCart"> 
<input type="submit" value="Detail" name="detail" class="detail">
</form>

<?php

    if (isset($_POST['detail'])) {
        session_start();
        print_r($_POST['detailCart']);
        echo "<br>";
        print_r($_POST['detailCart'][1]);
         // should result is one
    }
?>

1 个答案:

答案 0 :(得分:1)

  

首先将数组存储在 SESSION 中,这样您就可以在表单中的任何位置获得 SESSION DATA ,而另一种形式无需将数据提交到外部。

通过会话获取数据

cv.delegate= self

没有会话提取数据

<?php

    if (isset($_POST['detail'])) {
      echo $_SESSION['array1'][0];
      echo $_SESSION['array1'][1];
      echo $_SESSION['array1'][2];
   }
?>

O / P

<?php 
    $_SESSION['array1'] = array("zero","one","two");
    print_r($_SESSION['array1']);
?>
<form action="" method="post">
<input type="hidden" value="<?php echo implode(',',$_SESSION['array1']); ?>" name="detailCart"> 
<input type="submit" value="Detail" name="detail" class="detail">
</form>

<?php

    if (isset($_POST['detail'])) {
        session_start();
        echo $_POST['detailCart']; //string
        $array = explode(",",$_POST['detailCart']);
        echo '<br/>';
        echo $array[0];     
        echo '<br/>';
        echo $array[1];
        echo '<br/>';
        echo $array[2];

    }
?>

另一种方式

zero,one,two
zero
one
two
  

使用具有不同索引的相同名称创建输入字段数组   比如name ='name [0]',name ='name [1]',name ='name [2]'

提交后

 <?php 
        $_SESSION['array1'] = array("zero","one","two");
        print_r($_SESSION['array1']);
    ?>
    <form action="" method="post">

    <input type="hidden" value="<?php echo $_SESSION['array1'][0] ?>" name="detailCart[0]"> 

    <input type="hidden" value="<?php echo $_SESSION['array1'][1] ?>" name="detailCart[1]"> 

   <input type="hidden" value="<?php echo $_SESSION['array1'][2] ?>" name="detailCart[2]"> 
    <input type="submit" value="Detail" name="detail" class="detail">
    </form>