Thursday, 30 August 2018

How to empty the contents of a key belonging to an associative array without disabling the key in PHP?

I've an array titled $aFilter as follows(Output of print_r($aFilter);):

Currently only one internal array element is shown but it can contain many such elements. So consider the case with multiple internal array elements.
Array(
          [0] => Array(
            [filter_id] => 84
            [user_id] => 984
            [item_id] => 244
            [type_id] => user
            [feed_type] => a:10:{s:5:\"pages\";i:1;s:5:\"photo\";i:1;s:4:\"link\";i:1;s:5:\"event\";i:1;s:6:
\"friend\";i:1;s:11:\"user_status\";i:1;s:4:\"poll\";i:1;s:4:\"quiz\";i:1;s:6:\"market\";i:1;s:4:\"apps
\";i:1;}
            [get_noti] => 0
            [time_stamp] => 1432271245
          )
        )

I want the following desired array :
Array(
          [0] => Array(
            [filter_id] => 84
            [user_id] => 984
            [item_id] => 244
            [type_id] => user
            [feed_type] =>
            [get_noti] => 0
            [time_stamp] => 1432271245
          )
        )

So I tried following code but it returned me the same array upon print_r($aFilter);
$aFilter['feed_type'] = '';

Why so? Can someone please help me in my issue?
Thanks.

This will do the trick:
array_walk( $aFilter, function(&$var){
    $var['feed_type'] = '';
});

0 comments:

Post a Comment