0

我试图删除数组中以 [1] 开头的所有项目的完整级别

我对数组一点运气都没有。对我来说有点新。阅读我发现的所有东西都不清楚,也不清楚我的例子。

继承人我的代码到目前为止。我可以删除提交。但我不能再进一步了,而且我不知道如何在标题、特殊、项目等中删除所有这些作为项目 [1] - 常规凯撒沙拉

感谢您提供的任何帮助

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;
}
?>

想要的输出?:

sample of desired output
$testarray =    
Array(
        "heading" => Array
        (
            "0" => 'Salads',
            "1" => 'Pasta',
        ),

        "special" => Array
        (
            "0" => '',
            "1" => '',
        ),

        "item" => Array
        (
            "0" => 'Small Green',
            "1" => 'Baked Lasagna',
        ),

        "description" => Array
        (
            "0" => 'Grape tomatoes, onions, green peppers and cucumbers on the bed of crisp lettuce.',
            "1" => 'With meat sauce, tomato vegetarian sauce or Alfredo sauce',
        ),

        "price" => Array
        (
            "0" => 'See Desc',
            "1" => '$9.69',
        ),

        "notes" => Array
        (
            "0" => 'Available in Small ($2.99), Regular ($5.99)',
            "1" => '',
        ),

        "submit_val" => 'Submit'
    );
4

3 回答 3

1

如果我正确理解您的问题,您正在寻找删除任何数组键 [1]。如果您将其作为函数执行,则可以使用 unset 或声明并返回一个新数组。使用 unset() 可能是您正在寻找的,但它在函数中有点浪费时间,因为它只传递一个局部变量。相反,您可能希望将值传递到新数组。请注意,这将保留键值。

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;
}

如果 $key 不是 $searchKey,也就是 removeItemsRecursive($array,1); ,这实际上将遍历 $array 的每个值并将该值传递给 $newArray 将返回一个没有任何值的新数组,键为 1。

调用函数

要调用该函数,只需添加:

$array = removeItemsRecursive($testarray,1);

查看这个新数组时,您将看到您正在寻找的结果。

echo "<PRE>";
print_r($array);
echo "</PRE>";
//returns the array you're looking for.
于 2013-08-11T21:57:00.763 回答
0
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
}

$arrBetter = removeLevel($testarray, 2)将返回删除了该级别数据的数组!

于 2013-08-11T21:55:23.973 回答
0

http://php.net/manual/en/function.unset.php

在 php 中使用 unset 函数。在您的情况下,您可以遍历数组并删除位置 1 的所有元素。

于 2013-08-11T22:06:31.397 回答