Php 删除阵列的完整级别
我正在尝试删除阵列中的完整级别 以[1]开头的所有项目 我在数组方面一点运气都没有。对我来说有点陌生。阅读我发现的所有东西都不清楚,也不像我的例子 这是到目前为止我的密码。我可以删除提交。但我不能再往前走了,而且我不知道如何将标题、特色菜、项目等中的所有菜都作为项目[1]——普通凯撒沙拉删除 谢谢你提供的任何帮助Php 删除阵列的完整级别,php,Php,我正在尝试删除阵列中的完整级别 以[1]开头的所有项目 我在数组方面一点运气都没有。对我来说有点陌生。阅读我发现的所有东西都不清楚,也不像我的例子 这是到目前为止我的密码。我可以删除提交。但我不能再往前走了,而且我不知道如何将标题、特色菜、项目等中的所有菜都作为项目[1]——普通凯撒沙拉删除 谢谢你提供的任何帮助 code <?php // version: $testarray = Array( "heading" => Array
code
<?php
// version:
$testarray =
Array(
"heading" => Array
(
"0" => 'Salads',
"1" => 'Salads',
"2" => 'Pasta',
),
"special" => Array
(
"0" => '',
"1" => '',
"2" => '',
),
"item" => Array
(
"0" => 'Small Green',
"1" => 'Regular Caesar',
"2" => 'Baked Lasagna',
),
"description" => Array
(
"0" => 'Grape tomatoes, onions, green peppers and cucumbers on the bed of crisp lettuce.',
"1" => 'Classic recipe with romaine lettuce and croutons',
"2" => 'With meat sauce, tomato vegetarian sauce or Alfredo sauce',
),
"price" => Array
(
"0" => 'See Desc',
"1" => '$5.99',
"2" => '$9.69',
),
"notes" => Array
(
"0" => 'Available in Small ($2.99), Regular ($5.99)',
"1" => '',
"2" => '',
),
"submit_val" => 'Submit'
);
echo "testarray";
echo "<pre>";
print_r($testarray);
echo "</pre>";
echo "<hr>";
$removed_data=removeItem($testarray,'Submit');
echo "removed_data";
echo "<pre>";
print_r($removed_data);
echo "</pre>";
echo "<hr>";
die();
// works for submit
function removeItem($look_in_Array, $remove){
foreach($look_in_Array as $key => $value) {
echo $key . ' = ' . $value . '<br>';
if ($value ==$remove) {
echo 'found '.$remove."<br>";
unset($look_in_Array[$key]);
}
}
return $look_in_Array;
}
?>
$arrBetter=removeLevel($testarray,2)
将返回删除了该级别数据的数组 如果我正确理解了您的问题,您希望删除任何数组键[1]。如果您还是作为一个函数执行此操作,则可以使用unset或声明并返回一个新数组。使用unset()可能是您想要的,但在函数中使用它有点浪费时间,因为它只传递局部变量。相反,您可能希望将值传递到新数组。请注意,这将保留关键值
echo "<PRE>";
print_r($array);
echo "</PRE>";
//returns the array you're looking for.
如果$key不是$searchKey,也就是removietemsrecursive($array,1),那么它将遍历$array的每个值,并将该值传递给$newArray;将返回一个不带任何值的新数组,其键为1
调用函数
要调用该函数,只需添加:
查看此新阵列时,您将看到所需的结果
echo”“;
打印(数组);
回声“;
//返回要查找的数组。
在php中使用unset函数。在您的情况下,您可以迭代数组并删除位置1处的所有元素。您想要什么样的最终结果?如@devlshone所问,您能否提供一个示例,说明您的结果数组应该是什么样的(可能基于您问题中的示例数组)?这是否只做像提交这样的一级工作?我正在寻找一种方法来完成(添加的)所需的输出。我问你是因为你提到它不做多级传递一个值,如果我理解这个权利,不,这是递归的。注意函数中对函数的调用。循环,直到函数没有其他东西可解析为止。如果您想解析多级数组,递归函数是一件非常重要的事情,尤其是当您不知道该数组可能有多深或不知道如何简化代码时。编辑以反映如何调用它。效果很好,但它不会删除[0]项[0]小绿色拉会删除所有其他项。谢谢你的评论,这对你很有帮助!if($key!=$searchKey)实际上应该是if($key!=$searchKey)。这意味着它并不严格等同于。PHP松散地检查此项将0读取为false。我们要严格检查数值。如果你重新复制,它会工作。哦,我喜欢这个,我能理解它…我要测试这个oneParse错误:语法错误,意外的“[”在-$removed=[]行中;致命错误:无法在-unset($inner[$l]中取消字符串偏移量);//删除只需注释掉那一行。无论如何,它不会更改原始数组。k现在可以工作,但我需要的是已删除项返回的数组,而不是已删除项返回的数组。
function removeLevel($a, $l) { // $a = entire array, $l = level to remove
$removed = array();
foreach ($a as $inner) { // iterate through the outer array
array_splice($inner,$l,1); // remove the "$l"th element of the $inner array
}
return $a; // return new array with level removed
}
function removeItemsRecursive($array,$searchKey) {
/*
** Finds any key value that equals $searchKey
** in a multi-level array and does not pass anything
** equal to $searchKey.
*/
foreach($array AS $key=>$value) { //go through each array and assign [key] => value to $key and $value, respectively
if($key !== $searchKey) //if a key is not equal to the $searchKey (aka 1 for example) then assign something to $newArray
{
if(is_array($value)) //if the value inside of the array is another array (for multilevel arrays)
{
$newArray[$key] = removeItemsRecursive($value,$searchKey); //loop through the function and do this for the next level down.
}
else //if the value inside of the array is scalar
{
$newArray[] = $array[$key]; //the new array is assigned the current value where the array key is not equal to $searchKey.
}
}
}
return $newArray;
}
$array = removeItemsRecursive($testarray,1);
echo "<PRE>";
print_r($array);
echo "</PRE>";
//returns the array you're looking for.