从关联数组数组中删除所选元素

时间:2022-07-19 12:13:06

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.

当我运行代码时,Apache Web服务器崩溃了。

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

聪明的成员能指出我做错了什么吗?非常感谢你。

2 个解决方案

#1


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

这是一个工作演示:演示

#2


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

#1


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

这是一个工作演示:演示

#2


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