php - 从PHP(Laravel)中另一个数组中的数组中删除一些键
问题描述
如何从 PHP 中另一个数组中的数组中删除一些键?我有这个结构:
array (
0 =>
array (
'num' => '123',
'nome' => 'test 001'
'pontos' => 68,
'data_status' => '03/09/2021 10:05',
'uuid_status' => '69450ea451ae11ea85ca309c23d3a0ed'
),
1 =>
array (
'num' => '345',
'nome' => 'test 002'
'pontos' => 120,
'data_status' => '27/08/2021 15:46',
'uuid_status' => '3cbf4fd15d5411ea86956eef5d66cb13',
),
)
并且需要返回如下内容:
array (
0 =>
array (
'num' => '123',
'nome' => 'test 001'
'pontos' => 68
),
1 =>
array (
'num' => '345',
'nome' => 'test 002'
'pontos' => 120
)
)
我已经看到了一些答案,但它们似乎已经过时了,我也在使用 Laravel,所以如果有人从框架中指出我的一些东西以及它在纯 PHP 中的等价物,那将会有所帮助
解决方案
一个简单的 foreach 循环是一个很好的起点,注意这将通过使用对&
数组的引用而不是普通的简单副本从原始数组中删除项目。在$val
中foreach ( $input as &$val){
$input = [
[
'num' => '123',
'nome' => 'test 001',
'pontos' => 68,
'data_status' => '03/09/2021 10:05',
'uuid_status' => '69450ea451ae11ea85ca309c23d3a0ed'
],[
'num' => '345',
'nome' => 'test 002',
'pontos' => 120,
'data_status' => '27/08/2021 15:46',
'uuid_status' => '3cbf4fd15d5411ea86956eef5d66cb13'
]
];
$remove = ['data_status','uuid_status'];
foreach ( $input as &$val){
foreach ($val as $k => $v) {
if ( in_array($k,$remove) ) {
unset( $val[$k]);
}
}
}
print_r($input);
结果
Array
(
[0] => Array
(
[num] => 123
[nome] => test 001
[pontos] => 68
)
[1] => Array
(
[num] => 345
[nome] => test 002
[pontos] => 120
)
)
推荐阅读
- r - 删除R中列表中每个小标题的第一列
- python - 无法使用 twine 将二进制轮子上传到 pypi
- mongodb - 如何在 Mongodb 社区版中获取 CRUD 操作的日志详细信息?
- node.js - Mongoose 在第一次连接时无法连接到单个副本集
- jenkins - 当条件为真时,从参数化项目运行另一个管道
- java - Spring - 下载小于 1 kb 的小文件时出错
- javascript - 用于Mozilla的React Js文本输入光标位置
- c - 如何在 c 的 printf() 函数中使用字符串变量?
- javascript - 如何使用脚本编辑器在谷歌工作表中选择整行并以不同颜色突出显示?
- javascript - 基于 PHP 的大型数据集表单自动完成