在PHP中取消多维数组 [英] Unsetting multi-dimensional arrays in php
问题描述
我正在尝试在php的会话数组中取消设置某个值.我想问一下是否有更好的方法可以做到这一点:
I'm trying to unset a certain value in a session array in php. I would like to ask if there's a better way in doing this:
<?php
session_start();
if(isset($_GET['Uname'])){
echo "Uname is set!";
$uname=$_GET['Uname'];
echo count($_SESSION['user']);
for($x=0; $x < count($_SESSION['user']); $x++ ){
if($_SESSION['user'][$x]['Uname']==$uname){
unset($_SESSION['user'][$x]['Uname']);
}
}
}else{
}
?>
是否可以使用foreach循环来完成同一件事?或其他方法
Is it possible to accomplish the same thing using a foreach loop?Or another method
推荐答案
确定取消设置user
应该可以解决该问题.您不需要循环.尝试执行此操作,刷新页面肯定会一次设置值,而其他未设置它的值.
Sure unsetting user
should solve that. You don't need a loop. Try this, refreshing page will surely set value at one time and other unset it's value.
<?php
session_start();
$array = array('arr', 'arr', 'arr', 'arr', 'arr', 'arr');
if(isset($_SESSION['user']))
{
print_r($_SESSION['user']);
unset($_SESSION['user']);
}
else{
$_SESSION['user'] = $array;
echo "user session was set";
}
根据此问题, https://stackoverflow.com/questions/4891301/top -bad-practices-in-php 中,在循环中使用count()
是一个坏习惯.
And according to this question, https://stackoverflow.com/questions/4891301/top-bad-practices-in-php , using count()
in loop is a bad practice.
这篇关于在PHP中取消多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!