Unsetting multi-dimensional arrays in 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开发者_C百科['user'][$x]['Uname']);
}
}
}else{
}
?>
Is it possible to accomplish the same thing using a foreach loop?Or another method
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";
}
And according to this question, https://stackoverflow.com/questions/4891301/top-bad-practices-in-php , using count()
in loop is a bad practice.
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.
I can assure you that the best way to unset a variable is to use unset()
function on it.
精彩评论