unset不工作的PHP数组 [英] unset Not Working PHP Array

查看:71
本文介绍了unset不工作的PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在会话变量中的数组。加载此页面时,会给出一个id。我需要遍历数组并找到该变量,然后从数组中删除该元素。我试过使用unset,我不知道我的逻辑中是否有错误,但我似乎无法让它工作。我整天都在这里,而且这个页面几乎回应了所有的东西。任何可以提供帮助的人都会受到赞赏。

I have an array I'm storing in a session variable. When this page is loaded, an id is given. I need to loop through the array and find that variable, then delete that element from the array. I've tried using unset, and I don't know if there's an error in my logic somewhere, but I can't seem to get this to work. I've been at it all day, and have the page echoing out pretty much everything. Anyone who could help would be appreciated.

<?php
	session_start();
	
	$id = $_GET["id"];
	$name = $_GET["name"];
	$action = $_GET["action"];
	
	if (empty($_SESSION["people"])) {
		$_SESSION["people"] = array();
	}
	
	$people = $_SESSION["people"];
	
		$i = 0;
		echo "<p>Test: " . $people[0][0] . "</p>";
		echo "<p>Before Array: ";
		var_dump($_SESSION["people"]);
		echo "</p>";
		
		foreach ($people as $item) {
			echo "<p>Searching for: " . $id . "</p>";
			echo "<p>Current ID: " . $item[0] . "</p>";
			if ($item[0] == $id) {
				echo "Removing: " . $i;
				unset($people[$i]);
			} else {
				$i++;
			}
		}
		
		echo "<p>After Array: ";
		var_dump($_SESSION["people"]);
		echo "</p>";

		$_SESSION["people"] = $people;
	}

	foreach($people as $item) {
		echo "
			<input style=\"border: 1px solid #b97819; padding: 0 2px 0 2px; border-radius: 6px;\" type=\"button\" value=\"" . $item[1] . "\" onClick=\"removeContact(" . $item[0] . ");\" />
		";
?>

推荐答案

id =
id =


_GET [ id];
_GET["id"];


name =
name =


这篇关于unset不工作的PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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