我有以下数组:
Array
(
[0] =Array(
[date] =2016-09-16
[data] = Array(
[0] =Array
(
[ID] =1945
[Debit] =CREDIT
[timestamp] =1474025139
[LastName] =test1
[FirstName] =test1
)
[1] =Array
(
[ID] =1946
[Debit] =CREDIT
[timestamp] =1474025140
[LastName] =test2
[FirstName] =test2
)
[2] =Array
(
[ID] =1947
[Debit] =CREDIT
[timestamp] =1474025130
[LastName] =test3
[FirstName] =test3
)
)
[start_date] =2016-09-16
[end_date] =2016-09-16
[show_fee] =0
)
[1] =Array
(
[date] =2016-09-15
[data] = Array
(
[0] =Array
(
[ID] =1955
[Debit] =CREDIT
[timestamp] =1474025159
[LastName] =test11
[FirstName] =test11
)
[1] =Array
(
[ID] =1956
[Debit] =CREDIT
[timestamp] =1474025150
[LastName] =test22
[FirstName] =test22
)
[2] =Array
(
[ID] =1957
[Debit] =CREDIT
[timestamp] =1474025150
[LastName] =test33
[FirstName] =test33
)
)
[start_date] =2016-09-16
[end_date] =2016-09-16
[show_fee] =0
)
)
现在我想用特定的键从上面的数组中过滤一个数组。 例如,我想要时间戳和ID。我们可以假设id / timestampd是关键,反之亦然。
我尝试过array_column但没有成功。
答案 0 :(得分:0)
确定完全需要输出的内容并不容易。
一般原则可能使用以下规则适用的假设:
id
和timestamp
键值是唯一保留的子数组基于这些规则,这是一个适用于任何结构的通用解决方案(特别是无论搜索键出现的深度级别如何):
function select($input) {
$keys = ['ID', 'timestamp'];
return is_array($input)
? (array_keys($intersect = array_intersect_key($input, array_flip($keys))) == $keys
? $intersect
: array_map('select', $input)
)
: $input;
}
$output = select($input);
(测试)函数使用以下方法:
$input
,如果它不是数组,则按原样返回$input
中查找搜索到的键并且:
$input