从php的多级数组中删除指定的索引

时间:2013-07-19 19:52:53

标签: php

我在数组中使用多级数组,数组。我有一个索引“|”我使用它只是克隆表单元素,但当我保存表单时,它成为我的PHP数组的一部分。请让我知道如何用短代码删除它。我也使用PHP函数“array_walk”和“array_walk_recursive”但没有成功,下面显示我的数组数组

Array
(
[banner_heading] => Get Started Here!
[banner_text] => <pre id="line1">aaaa</pre>
[banner_button] => [button_blue link="#"]Buy Now[/button_blue]
[banner_media] => image
[upload_banner] => /wp-content/uploads/2013/07/videoImg.jpg
[youtube_video] => Oo3f1MaYyD8
[vimeo_video] => 24456787
[intro_heading] => Anyone Can Accept Credit Cards with Square
[intro_text] => 
[feature_content] => Array
    (
        [1] => Array
            (
                [title] => Free Secure Card Reader
                [description] => Sign up and we’ll mail you a free card reader. The reader fits right in your pocket and securely encrypts every swipe.
                [link] => #
                [icon] => /wp-content/uploads/2013/07/cardIcon.jpg
            )

        [2] => Array
            (
                [title] => Easy Setup
                [description] => Download the free Square Register app and link your bank account. No setup fees or long-term contracts. You’ll be accepting payments on your smartphone or iPad in minutes.
                [link] => #
                [icon] => /wp-content/uploads/2013/07/easyIcon.jpg
            )

        [3] => Array
            (
                [title] => Simple Pricing
                [description] => Pay just 2.75% per swipe for all major credit cards or a flat monthly $275. No other fees—so you know exactly what you pay. Square’s pricing fits businesses of all sizes.
                [link] => #
                [icon] => /wp-content/uploads/2013/07/pricingIcon.jpg
            )

        [5] => Array
            (
                [title] => 
                [description] => 
                [link] => 
                [icon] => 
            )

        [|] => Array
            (
                [title] => 
                [description] => 
                [link] => 
                [icon] => 
            )

    )
)

2 个答案:

答案 0 :(得分:1)

您可以使用unset

$array; // this is your array

unset($array['feature_content']['|']);

答案 1 :(得分:0)

以下函数可用于递归地将数组中的路径获取到键为|的节点。它返回一个以字符串分隔的冒号连接的路径节点数组,例如$out = array('key1:key2:|', 'key3:|');

function get_path($arr, $path = array()) {
    $out = array();
    foreach ($arr as $key => $value) {
        $full = array_merge($path, array($key));
        if ($key === '|') {
            $out[] = join(':', $full);
        } elseif (is_array($value)) {
            $out = array_merge($out, get_path($value, $full));
        }
    }
    return $out;
}

完成后,您可以按如下方式清理阵列:

$out = $this->get_path($arr);
if ( ! empty($out)){
    foreach ($out as $str){
        $path = explode(':', $str);
        array_pop($path);
        $nester = &$arr;
        foreach ($path as $node){
            $nester = &$nester[$node];
        }
        unset($nester['|']);
    }
}
print_r($arr);