我有一个PHP多维数组,其中有一个键,数组下有日期。
Array (
[123] => Array
(
[one] => 2016-01-22 10:32:15
[two] => 2016-02-21 14:24:15
[three] => 2016-02-12 11:00:15
)
[553] => Array
(
[one] => 2016-03-22 10:32:15
[two] => 2016-02-21 14:24:15
[three] => 2016-08-12 11:00:15
)
)
我需要在给定的月份得到密钥。例子:
$value = '2016-02';
return 123 -> 2
return 552 -> 1
实现这一目标的最佳方法是什么?谢谢你。
你可以像下面这样做:-
<?php
$array = Array (
'123' => Array
(
'one' => '2016-01-22 10:32:15',
'two' => '2016-02-21 14:24:15',
'three' => '2016-02-12 11:00:15'
),
'553' => Array
(
'one' => '2016-03-22 10:32:15',
'two' => '2016-02-21 14:24:15',
'three' => '2016-08-12 11:00:15'
)
);
$value = '2016-02';
$final_data = array();
foreach ($array as $key=> $arr){
foreach($arr as $key1=>$val){
if(strpos($val,$value)!==false){
$final_data[$key][]=$key1;
}
}
}
echo "<pre/>";print_r($final_data);
输出:https://eval.in/627935
或者这个也很有用:-
https://eval.in/627936您可以简单地如下所示。我稍微改了一下代码
<?php
$date = Array (
123 => Array
(
"2016-01-22 10:32:15",
"2016-02-21 14:24:15",
"2016-02-12 11:00:15"
),
553 => Array
(
"2016-03-22 10:32:15",
"2016-02-21 14:24:15",
"2016-08-12 11:00:15"
)
);
$givenDate = "2016-02";
$givenMonth = explode("-",$givenDate)[1];
foreach ($date as $key => $value){
foreach ($value as $k => $v){
$d = explode(" ", $v);
$d = explode("-", $d[0]);
if($d[1] ==$givenMonth ){
echo $key."=>".$k."<br/>";
}
}
}