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.
3
Answers
Please try this, this code can search for deapth of 2.
Hope this help you.