1

我正在使用多级数组,数组内的数组。我有一个索引“|” 我只使用它来克隆表单元素,但是当我保存表单时,它成为我的 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] => 
            )

    )
)
4

2 回答 2

1

您可以使用unset

$array; // this is your array

unset($array['feature_content']['|']);
于 2013-07-19T19:55:13.387 回答
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);
于 2013-07-19T22:17:43.230 回答