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

3 Answers 3

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]); } 
Sign up to request clarification or add additional context in comments.

2 Comments

My apologies I should provide more information. How do you disable 2 out of 3 array? For example just disable ['xfer'] and ['active']?
aahh foreach. I already learned this in my class. Thank you very much sir/madam. You a time saver :)
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']); ?> 

Comments

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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.