php - 从两个值之间的多维数组中获取一个数组
问题描述
我有一个这样的数组:
Array
(
[0] => Array
(
[date] => 1523752578
[weight1] => 1890
[weight2] => 1760
)
[1] => Array
(
[date] => 1523756192
[weight1] => 1890
[weight2] => 1760
)
[2] => Array
(
[date] => 1523759807
[weight1] => 1890
[weight2] => 1760
)
[3] => Array
(
[date] => 1523763423
[weight1] => 1890
[weight2] => 1760
)
)
如何从数组中获取两个 [date] 值之间的数组?
一个例子,第一个值是1523756192
,第二个值是1523763423
,应该返回一个包含[1]
,[2]
和[3]
但不是[0]
的数组,并且该数组应该仍然包含[weight1]
and[weight2]
“to”和“from”值将来自两个输入,用户选择两个日期。然后我会从两个日期之间的父数组中选择所有子数组。
解决方案
I think you are looking for array_filter.
function filterbydate($arr, $lowdate, $highdate) {
return array_filter($arr, function($val) use ($lowdate, $highdate) {
return $val['date'] >= $lowdate && $val['date'] <= $highdate;
});
}
// your data
$arr = [['date' => 1523752578,
'weight1' => 1890,
'weight2' => 1760],
['date' => 1523756192,
'weight1' => 1890,
'weight2' => 1760],
['date' => 1523759807,
'weight1' => 1890,
'weight2' => 1760],
['date' => 1523763423,
'weight1' => 1890,
'weight2' => 1760]];
var_dump(filterbydate($arr, 1523756192, 1523763423));
Please note, this retains index values, so the results will have indices of 1, 2, and 3. If you want them renumbered, you can use array_values on the resultant array - so replace the filterbydate internals with:
return array_values(
array_filter($arr, function($val) use ($lowdate, $highdate) {
return $val['date'] >= $lowdate && $val['date'] <= $highdate;
}));
推荐阅读
- javascript - 我已经在我的项目@react-native-paper 中安装了一个新的依赖项,所以当尝试使用命令 expo start 再次运行我的项目时,它会显示一个错误
- django - 如何向扩展另一个表单的表单添加/排除字段
- c++ - 尝试创建向量向量时出现 C++ 分段错误
- python - 如果它们是使用循环制作的,我如何对python中的复选框的值求和?
- java - 数组列表
> 无法转换为列表 - >
- docker - 如何在 docker stack 中按名称引用容器?
- python - 当我们有多个特征时,如何绘制散点图以了解数据的总体趋势
- python - 如何在 Python 中生成一串数字?
- azure-active-directory - InteractionRequiredAuthError: AADSTS65001: 用户或管理员未同意使用带有 ID 的应用程序
- java - 动态添加类型到 Annotation 处理环境