2

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']?

sg552
  • 1,521
  • 6
  • 32
  • 59

3 Answers3

3

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]);
}
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • My apologies I should provide more information. How do you disable 2 out of 3 array? For example just disable ['xfer'] and ['active']? – sg552 Oct 24 '11 at 19:43
  • aahh foreach. I already learned this in my class. Thank you very much sir/madam. You a time saver :) – sg552 Oct 24 '11 at 19:58
1

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.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
  • My apologies I should provide more information. How do you disable 2 out of 3 array? For example just disable ['xfer'] and ['active']? – sg552 Oct 24 '11 at 19:43
1

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']);
?>
jprofitt
  • 10,874
  • 4
  • 36
  • 46