Home  >  Article  >  Backend Development  >  How to delete multidimensional array in php

How to delete multidimensional array in php

藏色散人
藏色散人Original
2020-10-13 10:02:092950browse

php method to delete a multi-dimensional array: first create a PHP sample file; then delete the specified key-value pairs in the complex multi-dimensional array through the unsetMultiKeys method; finally check the running results.

How to delete multidimensional array in php

Recommended: "PHP Video Tutorial"

php deletes values ​​in multi-dimensional arrays

I found in the manual that after the transformation, it became a function that can delete specified key-value pairs in complex multi-dimensional arrays!

<?php
$arr = [
    &#39;test&#39; => &#39;value&#39;,
    &#39;level_one&#39; => [
        &#39;level_two&#39; => [
            &#39;level_three&#39; => [
                &#39;replace_this_array&#39; => [
                    &#39;special_key&#39; => &#39;replacement_value&#39;,
                    &#39;key_one&#39; => &#39;testing&#39;,
                    &#39;key_two&#39; => &#39;value&#39;,
                    &#39;four&#39; => &#39;another value&#39;,
                ],
            ],
            &#39;ordinary_key&#39; => &#39;value&#39;,
        ],
    ],
];
$unset = array(&#39;special_key&#39;, &#39;ordinary_key&#39;, &#39;four&#39;);
echo "<pre class="brush:php;toolbar:false">";
print_r(unsetMultiKeys($unset, $arr));
print_r($arr);
echo "
"; 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(); }

Run result:

How to delete multidimensional array in php

Change the key-value pairs in the multi-dimensional array

The above is the detailed content of How to delete multidimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn