如何从关联数组中删除所有特定键 PHP

我必须从如下关联数组中取消设置所有“优先级”键和值。还找不到解决此问题的方法。


您认为从整个数组中删除特定键值对的最佳方法是什么?


$countries = array(

    'AE' => array(

        'postcode' => array(

          'required' => false,

          'hidden'   => true,

          'priority' => 40,

        ),

        'city' => array(

            'priority' => 50,

        ),

    ),

    'AF' => array(

        'state' => array(

            'priority' => 65,

        ),

    ),

    'AO' => array(

        'postcode' => array(

            'required' => false,

            'hidden'   => true,

        ),

        'state'    => array(

            'label' => __( 'Province', 'woocommerce' ),

            'priority' => 70,

        ),

    ),

    // + another arrays

);

编辑


我发现的解决方案:


foreach( $countries as $country => $fields ) {

    foreach( $fields as $field => $options ) {

        if ( isset( $options['priority'] ) ) {

            unset( $countries[$country][$field]['priority'] );

        }

    }

}

但我仍然想知道是否有其他更好的选择,编写更少的代码,可能是一些预定义的函数。


函数式编程
浏览 95回答 3
3回答

蝴蝶不菲

<?php$countries = array(&nbsp; &nbsp; 'AE' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'postcode' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hidden'&nbsp; &nbsp;=> true,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'state'&nbsp; &nbsp; => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 50,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; 'AF' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'state' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 65,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; 'AO' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'postcode' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hidden'&nbsp; &nbsp;=> true,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'state'&nbsp; &nbsp; => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => ['Province', 'woocommerce'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 70,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; // + another arrays);foreach($countries as &$country) {&nbsp; &nbsp; if(array_key_exists('state', $country)&nbsp; &nbsp; &nbsp; &nbsp; && array_key_exists('priority', $country['state'])) {&nbsp; &nbsp; &nbsp; &nbsp; unset($country['state']['priority']);&nbsp; &nbsp; }}var_dump($countries);

一只甜甜圈

<?php$countries = array(&nbsp; &nbsp; 'AE' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'postcode' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hidden'&nbsp; &nbsp;=> true,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'state'&nbsp; &nbsp; => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 50,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; 'AF' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'state' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 65,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; 'AO' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'postcode' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hidden'&nbsp; &nbsp;=> true,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'state'&nbsp; &nbsp; => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => ['Province', 'woocommerce'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 70,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; // + another arrays);function unset_recursive(&$array, $key) {&nbsp; &nbsp; unset($array[$key]);&nbsp; &nbsp; foreach ($array as &$value) {&nbsp; &nbsp; &nbsp; &nbsp; if (is_array($value)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset_recursive($value, $key);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}unset_recursive($countries, 'priority');var_dump($countries);

开满天机

请尝试一下,此代码可以搜索 2 的 deapth。<?php$countries = array(&nbsp; &nbsp; 'AE' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'postcode' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hidden'&nbsp; &nbsp;=> true,&nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 50,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'state'&nbsp; &nbsp; => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 50,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; 'AF' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'state' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 65,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; 'AO' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'postcode' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hidden'&nbsp; &nbsp;=> true,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'state'&nbsp; &nbsp; => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => ['Province', 'woocommerce'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'priority' => 70,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; // + another arrays);$lookKey = 'priority';foreach($countries as $key => $country) {&nbsp; &nbsp; if(array_key_exists($lookKey, $country)){&nbsp; &nbsp; &nbsp; &nbsp; //echo "\n $key - $lookKey";&nbsp; &nbsp; &nbsp; &nbsp; unset($countries[$key][$lookKey]);&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; foreach($country as $subKey => $ar){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(array_key_exists($lookKey, $ar)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //echo "\n $key - $subKey - $lookKey";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($countries[$key][$subKey][$lookKey]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}print_r($countries);希望这对你有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP