Reputation: 319
How to unset() some parts of my multimensional array?
I have an array similar like
$list['A_1']['AA_2']['AAA_3']['AAAA_4'] = 'stuff';
$list['B_1']['BB_2']['BBB_3'] = 'stuff';
$list['C_1']['CC_2']['CCC_3']['CCCC_4']['CCCCC_5']['CCCCCC_6']['CCCCCCC_7']['CCCCCCCC_8'] = 'stuff';
$list['D_1']['DD_2']['DDD_3']['DDDD_4']['DDDDD_5'] = 'stuff';
Normally I would do my desired unset()-ing with
unset( $list['A_1']['AA_2']['AAA_3'] );
unset( $list['B_1']['BB_2']['BBB_3'] );
unset( $list['C_1']['CC_2']['CCC_3']['CCCC_4']['CCCCC_5'] );
unset( $list['D_1']['DD_2']['DDD_3']['DDDD_4'] );
However I'd like to get the same result by unsetting them using the elements from another array:
$result = array('A_1','AA_2','AAA_3');
$result = array('B_1','BB_2','BBB_3');
$result = array('C_1','CC_2','CCC_3','CCCC_4','CCCCC_5');
$result = array('D_1','DD_2','DDD_3','DDDD_4');
I've tried to approach it with the simplest way I could imagine but sadly it's not as easy as that and doesn't work:
$result = array('A_1','AA_2','AAA_3');
unset( $list[$result] );
returns: Warning: Illegal offset type in unset
Upvotes: 2
Views: 52
Reputation: 1633
This should be worked
function unsetArray(&$list, $result) {
eval("unset("."\$list['".implode("']['", $result)."']".");");
}
unsetArray($list, $result);
var_dump($list);
Upvotes: 1
Reputation: 106
You need to form multidemensional array to unset. Here you are using single dimensional array. Just form array inside array in your result array.
Thanks.
Upvotes: 0