php刪除多維數(shù)組的方法-創(chuàng)新互聯(lián)

這篇文章主要介紹了php刪除多維數(shù)組的方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)公司專注于梓潼企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),商城網(wǎng)站定制開發(fā)。梓潼網(wǎng)站建設(shè)公司,為梓潼等地區(qū)提供建站服務(wù)。全流程按需求定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

php刪除多維數(shù)組的方法:首先創(chuàng)建一個(gè)PHP示例文件;然后通過unsetMultiKeys方法刪除復(fù)雜的多維數(shù)組里面的指定鍵值對;最后查看運(yùn)行結(jié)果即可。

php刪除多維數(shù)組里面的值

在手冊里面發(fā)現(xiàn),改造后變成了一個(gè)函數(shù),可以刪除復(fù)雜的多維數(shù)組里面的制定鍵值對!

<?php
$arr = [
    'test' => 'value',
    'level_one' => [
        'level_two' => [
            'level_three' => [
                'replace_this_array' => [
                    'special_key' => 'replacement_value',
                    'key_one' => 'testing',
                    'key_two' => 'value',
                    'four' => 'another value',
                ],
            ],
            'ordinary_key' => 'value',
        ],
    ],
];
$unset = array('special_key', 'ordinary_key', 'four');
echo "<pre>";
print_r(unsetMultiKeys($unset, $arr));
print_r($arr);
echo "</pre>";
exit;
function unsetMultiKeys($unset, $array) {
    $arrayIterator = new \RecursiveArrayIterator($array);
    $recursiveIterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);
    foreach ($recursiveIterator as $key => $value) {
        foreach ($unset as $v) {
            if (is_array($value) && array_key_exists($v, $value)) {
                // 刪除不要的值
                unset($value[$v]);
                // Get the current depth and traverse back up the tree, saving the modifications
                $currentDepth = $recursiveIterator->getDepth();
                for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--) {
                    // Get the current level iterator
                    $subIterator = $recursiveIterator->getSubIterator($subDepth);
                    // If we are on the level we want to change, use the replacements ($value) other wise set the key to the parent iterators value
                    $subIterator->offsetSet($subIterator->key(), ($subDepth === $currentDepth ? $value : $recursiveIterator->getSubIterator(($subDepth + 1))->getArrayCopy()));
                }
            }
        }
    }
    return $recursiveIterator->getArrayCopy();
}

運(yùn)行結(jié)果:

php刪除多維數(shù)組的方法

改變多維數(shù)組里面的鍵值對

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享php刪除多維數(shù)組的方法內(nèi)容對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!

當(dāng)前名稱:php刪除多維數(shù)組的方法-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://muchs.cn/article18/dhsgdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站建設(shè)商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)