我有这个数组:
Array
(
[0] => Array
(
[id] => 15bw83nfo3wg4
[date] => 2019-01-01
[d_c] => D
[D] => 76181734.95
[C] => 0.00
)
[1] => Array
(
[id] => 15byrr7fo3wg4
[date] => 2019-01-02
[d_c] => D
[D] => 4000000.00
[C] => 0.00
)
[2] => Array
(
[id] => 15bzwubfo3wg4
[date] => 2019-01-02
[d_c] => K
[D] => 0.00
[C] => 1585500.00
)
[3] => Array
(
[id] => 15c0vlvfo3wg4
[date] => 2019-01-02
[d_c] => K
[D] => 0.00
[C] => 10000000.00
如何在以日期作为参数值的数组中搜索并返回包含参数值的 ALL 数组?
我已经尝试过了:
$date = '2019-01-02';
$key = array_search($date, array_column($data, 'date'));
$result = $data[$key];
但是我得到的结果仅仅是:
[1] => Array
(
[id] => 15byrr7fo3wg4
[date] => 2019-01-02
[d_c] => D
[D] => 4000000.00
[C] => 0.00
)
答案 0 :(得分:1)
使用array_filter函数
$result = array_filter($data, function($x) use ($date) { return $x['date'] == $date; });
答案 1 :(得分:0)
您只会得到一个结果,因为您的$key
变量仅返回找到的第一个元素,要获取多个,您需要使用:
array_keys(array_column($array, 'date'), "2019-01-02")
这将返回包含您要查找的数组中的键的键列表。
然后您可以遍历它们:
$array = [["id" => 1, "date" => "2019-01-02"],["id" => 2, "date" => "2019-01-03"],["id" => 3, "date" => "2019-01-02"],["id" => 4, "date" => "2019-01-05"]];
$date = '2019-01-02';
$keys = array_keys(array_column($array, 'date'), "2019-01-02");
$results = [];
foreach ($keys as $key) {
$results[] = $array[$key];
}
var_dump($results);