Question

I have the following array of associative arrays.

$result = array(
    (int) 0 => array(
        'name' => 'Luke',
        'id_number' => '1111',
        'address' => '1544addr',
        'time_here' => '2014-04-12 13:07:08'
    ),
    (int) 1 => array(
        'name' => 'Sam',
        'id_number' => '2222',
        'address' => '1584addr',
        'time_here' => '2014-04-12 14:15:26'

I want to remove selected elements from this array such that it will look like this;

array(
    (int) 0 => array(
        'name' => 'Luke',
        'id_number' => '1111'
    ),
    (int) 1 => array(
        'name' => 'Sam',
        'id_number' => '2222',

This is the code I wrote;

    foreach($result as $value) 
    {            
        unset($value('address')  );
        unset($value('time_here')  );
    } 

When I run the code, Apache web server crashed.

Can the smarter members point out what did I do wrong? Thank you very much.

Was it helpful?

Solution

Array notation is wrong, use this;

$finalResult = array();
foreach($result as $value) 
{            
    unset($value['address']  );
    unset($value['time_here']  );
    $finalResult[] = $value;
}

Here is a working demo: Demo

OTHER TIPS

That is because you are not accessing array correctly. Use square bracket insted of round brackets :

foreach($result as $value) 
    {            
        unset($value['address']  );
        unset($value['time_here']  );
    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top