Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.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_Arrays_Session - Fatal编程技术网

Php 更新特定键的数组值

Php 更新特定键的数组值,php,arrays,session,Php,Arrays,Session,我有一个包含动物的数组。我使用foreach循环打印出数组。我希望更新特定键的值,而不更改具有不同键的任何其他值。我在每个键旁边都有一个更新输入框,用于输入值。例如,当我为键0键入值时,所有其他键值也会更改,而不仅仅是0 所以我的输出是这样的: The value of $_SESSION['0'] is 'cat' // i want to change cat to something else The value of $_SESSION['1'] is 'dog'

我有一个包含动物的数组。我使用foreach循环打印出数组。我希望更新特定键的值,而不更改具有不同键的任何其他值。我在每个键旁边都有一个更新输入框,用于输入值。例如,当我为键0键入值时,所有其他键值也会更改,而不仅仅是0

所以我的输出是这样的:

The value of $_SESSION['0'] is 'cat'          // i want to change cat to something else
The value of $_SESSION['1'] is 'dog'          // dog to something else
The value of $_SESSION['2'] is 'mouse'        /mouse to something else
这是我的代码,因为我在foreach循环中的每个输入中都添加了$key,所以非常令人沮丧

**code 
    <?php
// begin the session
session_start();

// create an array
$my_array=array('cat', 'dog', 'mouse', 'bird', 'crocodile', 'wombat', 'koala', 'kangaroo');

// put the array in a session variable
$_SESSION['animals']=$my_array;





// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
{   

 // and print out the values
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';


// getting the updated value from input box
if (isset($_POST["submit"])) {
        $aaa = $_POST['aaa'];


// setting the session spesific session array value different for each key  
 $_SESSION['animals'][$key] = $aaa;
}
    ?>
<form method="post" action="testthis.php">

<input type="text" name="aaa" value="<?php echo $value ; ?>" size="2" />
<input type="submit" value="Add to cart" name="submit"/></div>

</form>
    <?php

}
echo"<br/>---------------------------------------------------------<br/>";
//echoing out the results again for the updated version
foreach($_SESSION['animals'] as $key=>$value)
{ 

// and print out the values
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';

}

?>
**代码
试试下面的代码

编辑:

1) 将
isset($\u POST[“submit”])
移动到
foreach
循环之外,使其不会更新所有会话

2) 使用要更新的键的值进行隐藏输入,这样我们就可以只关注一个要更新的键

 <?php
    // begin the session
    session_start();

    // create an array
    $my_array=array('cat', 'dog', 'mouse', 'bird', 'crocodile', 'wombat', 'koala', 'kangaroo');

    // put the array in a session variable
    if(!isset($_SESSION['animals']))
        $_SESSION['animals']=$my_array;

    // move submit code outside of foreach loop
    if (isset($_POST["submit"])) 
    {
        $aaa = $_POST['aaa'];
        $key_var = $_POST['ke'];

        // setting the session spesific session array value different for each key  
        $_SESSION['animals'][$key_var] = $aaa;
    }



    // loop through the session array with foreach
    foreach($_SESSION['animals'] as $key=>$value)
    {   

        // and print out the values
        echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';

        // getting the updated value from input box
        ?>
        <form method="post" action="">
            <input type="text" name="aaa" value="<?php echo $value ; ?>" size="2" />
            <!-- take a hidden input with value of key -->
            <input type="hidden" name="ke" value="<?php echo $key; ?>">
            <input type="submit" value="Add to cart" name="submit"/></div>
        </form>
        <?php
    }

    echo"<br/>---------------------------------------------------------<br/>";
    //echoing out the results again for the updated version
    foreach($_SESSION['animals'] as $key=>$value)
    { 
        // and print out the values
        echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }
?>


您需要为循环中的每个
赋予一个唯一的属性,即
哇!我真不敢相信我没想到。我现在就去试试。我真的希望它能起作用!!谢谢我可以问一下,为什么输入类型是隐藏的?因为输入框需要是可见的,以便能够用键的值更新,
foreach($\u SESSION['anies']as$key=>$value){//行和打印出的值回显“$\u SESSION[.”的值。“$key.”是“.”“$value”。
”;
没有打印
$\u SESSION[$key]
,他们正在打印
$\u会话['animals'][$key]
,这可能会误导您。它是隐藏的,这样它们的键就不会在值之外更改。您只会更改值,而不会更改键,就像您更改了键一样,您也可以添加一个新的键=>值。是的,它可以工作,值会更新到每个单独的键。感谢Suyog和所有人的输入!!!我将定义itely!只有一个问题,如果我将submit按钮从foreach循环中移到代码的底部。我能一次更新所有内容吗?因为每个内容都在更新,但当更新另一个内容时,该值会返回到未更新的值这是因为您已在页面开始处初始化了
$my_array
每次页面刷新时,数组都会初始化为初始值立即查看更新的代码。在将值分配给
$\u会话['animals']
之前,我已经添加了
if(!isset($\u SESSION['animals'])
。它将首先检查值是否分配给
$\u SESSION['animals']
或否,并且仅当之前未分配任何值时,才会分配值。。。!!