sg552
sg552

Reputation: 1543

Unset many array

I need help with my code. To unset xfer array from array below:

    if($_SESSION["s"]["user"]["typ"] == 'admin') {
    $form["tabs"]['dns_soa'] = array (
        'title'     => "DNS Zone",
        'width'     => 100,
        'template'  => "templates/dns_soa_edit.htm",
        'fields'    => array (
        ##################################
        # Begin Datatable fields
        ##################################

        'xfer' => array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'TEXT',
            'default'   => '',
            'value'     => '',
            'width'     => '30',
            'maxlength' => '255'
        ),

        'update_acl' => array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'TEXT',
            'default'   => '',
            'value'     => '',
            'width'     => '30',
            'maxlength' => '255'
        ),

        'active' => array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'CHECKBOX',
            'default'   => 'Y',
            'value'     => array(0 => 'N',1 => 'Y')
        ),
        ##################################
        # ENDE Datatable fields
        ##################################
        )
    );
    }



I just:

unset($form["tabs"]['dns_soa']['fields']['xfer']);



and to unset all 3 I do this. UPDATE - I have many array in 'fields' actually but I just provide 3:

unset($form["tabs"]['dns_soa']['fields']['xfer']);
unset($form["tabs"]['dns_soa']['fields']['update_acl']);
unset($form["tabs"]['dns_soa']['fields']['active']);



Is there anyway I could unset many array without coding unset($form["tabs"]['dns_soa']['fields']array name here); many times? Thanks in advance.

UPDATE - My apologies I should provide more information. How do you disable 2 out of 3 array? For example just disable ['xfer'] and ['active']?

Upvotes: 2

Views: 139

Answers (3)

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22152

If you want to unset all the subarray in fields, you can use:

unset($form["tabs"]['dns_soa']['fields']);

Edit: In this case the best you can do is to use a for or foreach.

Upvotes: 1

jprofitt
jprofitt

Reputation: 10964

If it's something you plan on using frequently, you could just create a function to help shorthand it:

<?php
function unset_array($keys, &$arr) {
    foreach($keys as $key) {
        unset($arr[$key]);
    }
}

unset_array(array('xfer', 'active'), $arr['tabs']['dns_soa']);
?>

Upvotes: 1

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43508

You can just say:

// to unset the parent "fields" array, which includes xfer, update_acl and active
unset($form["tabs"]['dns_soa']['fields']);

or

// just to reset the fields array
$form["tabs"]['dns_soa']['fields'] = array();

UPDATE:

To unset only a particular subset of the keys without repeating unset many times in the code, I would do a loop:

foreach (array('xfer', 'active') as $field) {
  unset($form["tabs"]['dns_soa']['fields'][$field]);
}

Upvotes: 3

Related Questions