Php 数组中未设置的元素
我试图从数组中删除一个键/值对,但它似乎不起作用。基本上,我进行了一个返回JSON的API调用。我也是这样做的Php 数组中未设置的元素,php,arrays,Php,Arrays,我试图从数组中删除一个键/值对,但它似乎不起作用。基本上,我进行了一个返回JSON的API调用。我也是这样做的 $tempArray = json_decode($projects, true); 如果我输出$tempArray,我会看到如下内容 array:2 [ 0 => array:9 [ "id" => 4 "name" => "Some Project Name" "value" => "234" "user_id" =&g
$tempArray = json_decode($projects, true);
如果我输出$tempArray,我会看到如下内容
array:2 [
0 => array:9 [
"id" => 4
"name" => "Some Project Name"
"value" => "234"
"user_id" => "1"
"client_id" => "97"
"contact" => "Jane Berry"
]
1 => array:9 [
"id" => 3
"name" => "Another Project Name"
"value" => "6"
"user_id" => "1"
"client_id" => "97"
"contact" => "John Doe"
]
]
我基本上需要删除value元素,所以我要这样做
unset($tempArray['value']);
如果我在unset之后输出$tempArray,它将显示与之前完全相同的值元素和值
我需要做什么才能从阵列中完全删除它
谢谢
取消设置
不会递归地查看子数组以删除键值
。只有在第一级具有名为value
的键时,才会删除该键。在数组中,一级键是:0和1。
因此,要从所有子数组中删除value
,您必须从数组中抛出所有项并unset
它。您可以通过一个简单的foreach
来实现这一点
foreach($tempArray as $key => $data) {
unset($data['value']);
$tempArray[$key] = $data; //Overwrite old data with new with value unset.
}
现在,子数组项中没有
值
键。取消设置
将不会递归地查看子数组以删除键值
。只有在第一级具有名为value
的键时,才会删除该键。在数组中,一级键是:0和1。
因此,要从所有子数组中删除value
,您必须从数组中抛出所有项并unset
它。您可以通过一个简单的foreach
来实现这一点
foreach($tempArray as $key => $data) {
unset($data['value']);
$tempArray[$key] = $data; //Overwrite old data with new with value unset.
}
现在在子数组项中没有value
键。根据,您没有名为“value”的键,它是数组中的顶级键。如果您的数组如下所示:
$myArray = array(
"value" => "My Value to delete",
"anotherKey" => "hello world",
);
unset($tempArray[0]['value']);
foreach($tempArray as &$nestedArray){
unset($nestedArray['value']);
}
然后你可以做unset($myArray['value'])
并删除键和值。在您的情况下,您要查找的键嵌套在数字键[0]
或[1]
下。您可以这样具体地引用这些:
$myArray = array(
"value" => "My Value to delete",
"anotherKey" => "hello world",
);
unset($tempArray[0]['value']);
foreach($tempArray as &$nestedArray){
unset($nestedArray['value']);
}
但我认为您希望实现的是从数组中删除任何键值的痕迹,在这种情况下,您最好执行以下操作:
$myArray = array(
"value" => "My Value to delete",
"anotherKey" => "hello world",
);
unset($tempArray[0]['value']);
foreach($tempArray as &$nestedArray){
unset($nestedArray['value']);
}
注意$nestedArray
前面的&
符号。这意味着“通过值传递”,实际上将在一行中更新$tempArray
,而不需要任何其他内容
进一步阅读:
根据,您没有名为“value”的键,它是数组中的顶级键。如果您的数组如下所示:
$myArray = array(
"value" => "My Value to delete",
"anotherKey" => "hello world",
);
unset($tempArray[0]['value']);
foreach($tempArray as &$nestedArray){
unset($nestedArray['value']);
}
然后你可以做unset($myArray['value'])
并删除键和值。在您的情况下,您要查找的键嵌套在数字键[0]
或[1]
下。您可以这样具体地引用这些:
$myArray = array(
"value" => "My Value to delete",
"anotherKey" => "hello world",
);
unset($tempArray[0]['value']);
foreach($tempArray as &$nestedArray){
unset($nestedArray['value']);
}
但我认为您希望实现的是从数组中删除任何键值的痕迹,在这种情况下,您最好执行以下操作:
$myArray = array(
"value" => "My Value to delete",
"anotherKey" => "hello world",
);
unset($tempArray[0]['value']);
foreach($tempArray as &$nestedArray){
unset($nestedArray['value']);
}
注意$nestedArray
前面的&
符号。这意味着“通过值传递”,实际上将在一行中更新$tempArray
,而不需要任何其他内容
进一步阅读:
在数组的第一级上没有名为“value”的键。您需要执行类似于unset($tempArray[0]['value'])的操作代码>否则循环。在数组的第一级上没有名为“value”的键。您需要执行类似于unset($tempArray[0]['value'])的操作代码>否则循环通过它。