0

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.

1
  • Your array notation is wrong. You need to use [] See my answer Commented Apr 23, 2014 at 5:22

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I have one more problem. How do I store the truncated array to $result? The answer removes the element from $value, not $result.
I have a follow-up question. stackoverflow.com/questions/23235855/… This time, I would like to filter selected elements and not remove them. This is more tricky. I don't even know what function to start with. At least, for removal, one can use unset(). Would you be able to help?
0

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']  );
    } 

2 Comments

It would be better, check already answered ones before posting your answer
@HüseyinBABAL Maybe you posted while i was typing. But anyways I would delete my answer so that you are the one who gets all the upvote :) Is that OK?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.