删除数组的完整级别

时间:2013-08-11 21:44:49

标签: php

我试图删除数组中的完整级别 所有以[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'
    );

3 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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)将返回删除了该级别数据的数组!

答案 2 :(得分:0)

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

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