0

I have to unset all 'priority' keys and values from an associative array like the one below. Couldn't find yet a solution to do this.

Which do you think is the best method to remove a specific key-value pair from an entire array?

$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
);

EDIT

Solution that I found:

foreach( $countries as $country => $fields ) {
    foreach( $fields as $field => $options ) {
        if ( isset( $options['priority'] ) ) {
            unset( $countries[$country][$field]['priority'] );
        }
    }
}

But I still wonder if there are other better options, with less written code, possibly some predefined functions.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

3 Answers3

0
<?php
$countries = array(
    'AE' => array(
        'postcode' => array(
          'required' => false,
          'hidden'   => true,
        ),
        'state'    => 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) {
    if(array_key_exists('state', $country)
        && array_key_exists('priority', $country['state'])) {
        unset($country['state']['priority']);
    }
}
var_dump($countries);
0
<?php
$countries = array(
    'AE' => array(
        'postcode' => array(
          'required' => false,
          'hidden'   => true,
        ),
        'state'    => 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
);

function unset_recursive(&$array, $key) {
    unset($array[$key]);
    foreach ($array as &$value) {
        if (is_array($value)) {
            unset_recursive($value, $key);
        }
    }
}

unset_recursive($countries, 'priority');
var_dump($countries);
0

Please try this, this code can search for deapth of 2.

<?php
$countries = array(
    'AE' => array(
        'postcode' => array(
          'required' => false,
          'hidden'   => true,
        'priority' => 50,
        ),
        'state'    => 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
);

$lookKey = 'priority';
foreach($countries as $key => $country) {

    if(array_key_exists($lookKey, $country)){
        //echo "\n $key - $lookKey";
        unset($countries[$key][$lookKey]);
    }else{
        foreach($country as $subKey => $ar){    
            if(array_key_exists($lookKey, $ar)){
                //echo "\n $key - $subKey - $lookKey";
                unset($countries[$key][$subKey][$lookKey]);
            }
        }
    }


}

print_r($countries);

Hope this help you.

mail2bapi
  • 1,547
  • 1
  • 12
  • 18