我是新的PHP!。
我有一个包含日期和值的数组。我如何获得所有的价值观 具体日期? EXP:我想从2015年获得所有价值。如果有人知道可以指导我。
答案 0 :(得分:1)
您可以使用array_filter,然后检查数组键substr是否与日期匹配。
<?php
$date = 2016;
$array = [
'2016-01-01' => 'a',
'2016-01-02' => 'b',
'2016-12-01' => 'c',
'2017-01-04' => 'd',
'2017-01-05' => 'e',
'2017-01-06' => 'f',
];
$result = array_filter($array, function ($key) use ($date) {
return substr($key, 0, strlen($date)) == $date;
}, ARRAY_FILTER_USE_KEY);
print_r($result);
<强>结果:强>
Array
(
[2016-01-01] => a
[2016-01-02] => b
[2016-12-01] => c
)
答案 1 :(得分:0)
<?php
$array = array('2015-01-01' => "test1",
'2013-02-04' => "test2",
'2011-03-08' => "test3",
'2016-03-08' => "test3");
foreach( $array as $key => $value ){
if(date('Y',strtotime($key)) >= 2015)
echo $value . ", ";
}
?>
答案 2 :(得分:0)
An Array Contains a Key and a Value. The Key in your case is the Date. And the Value is... the value
You define it like this
$array = array(
Date => Value,
AnotherDate => AnotherValue,
);
or since PHP5
$array = [
Date => Value,
AnotherDate => AnotherValue,
];
if you now look at your Array using
var_dump($array);
You'll see the date is already combined with your value and It will even show the type of value (boolean, string, integer...)
Edit Anotherone was faster