我需要使用PHP从值搜索中删除一些字符。我在下面解释我的代码。
当前数据
$datArr = array(
array(
"name" => "Ram",
"id" => 1,
"date" => "12/04/2017 05:31:57 AM"
),
array(
"name" => "Rahim",
"id" => 5,
"date" => "12/03/2017 12:31:57 PM"
),
array(
"name" => "Raj",
"id" => 4,
"date" => "12/04/2017 05:31:57 PM"
)
);
预期输出
$datArr = array(
array(
"name" => "Ram",
"id" => 1,
"date" => "12/04/2017 05:31:57"
),
array(
"name" => "Rahim",
"id" => 5,
"date" => "12/03/2017 12:31:57"
),
array(
"name" => "Raj",
"id" => 4,
"date" => "12/04/2017 05:31:57"
)
);
从上面的数组中有一个date
字段值,我需要使用PHP从所有日期删除AM/PM
。
答案 0 :(得分:2)
使用rtrim删除这样的尾部字符,
$datArr = array_map(function($v){
$v['date'] = rtrim($v['date'], " APMapm");
return $v;
}, $datArr);
答案 1 :(得分:0)
$datArr=array(array("name"=>"Ram","id"=>1,"date"=>"12/04/2017 05:31:57 AM"),array("name"=>"Rahim","id"=>5,"date"=>"12/03/2017 12:31:57 PM"),array("name"=>"Raj","id"=>4,"date"=>"12/04/2017 05:31:57 PM"));
使用foreach循环遍历数组。 当涉及到日期值时,请执行以下操作
将日期字符串转换为datetime对象
$time = strtotime($dateKeyValueInArray);
现在设置此时间戳的日期格式
$newformat = date('Y-m-d',$time);
使用
查看输出echo $newformat;
参考链接是 Converting string to Date and DateTime https://www.w3schools.com/php/func_date_date_format.asp
希望有所帮助:)
答案 2 :(得分:0)
您可以使用str_replace()
循环<{1}}来执行此操作
<强>代码强>
foreach
<强>输入强>
foreach ($datArr as &$dat) {
$dat['date'] = str_replace([' AM', ' PM'], "", $dat['date']);
}
<强>输出强>
$datArr = array(
array(
"name" => "Ram",
"id" => 1,
"date" => "12/04/2017 05:31:57 AM"
),
array(
"name" => "Rahim",
"id" => 5,
"date" => "12/03/2017 12:31:57 PM"
),
array(
"name" => "Raj",
"id" => 4,
"date" => "12/04/2017 05:31:57 PM"
)
);
答案 3 :(得分:0)
尝试使用foreach循环进行此操作
$datArr=array(array("name"=>"Ram","id"=>1,"date"=>"12/04/2017 05:31:57 AM"),array("name"=>"Rahim","id"=>5,"date"=>"12/03/2017 12:31:57 PM"),array("name"=>"Raj","id"=>4,"date"=>"12/04/2017 05:31:57 PM"));
print_r($datArr);
foreach($datArr as $key=>$value)
{
$datArr[$key]['date'] = date('m/d/Y H:i:s',strtotime($value['date']));
}
echo "after";
print_r($datArr);
此代码的输出
Array
(
[0] => Array
(
[name] => Ram
[id] => 1
[date] => 12/04/2017 05:31:57 AM
)
[1] => Array
(
[name] => Rahim
[id] => 5
[date] => 12/03/2017 12:31:57 PM
)
[2] => Array
(
[name] => Raj
[id] => 4
[date] => 12/04/2017 05:31:57 PM
)
)
afterArray
(
[0] => Array
(
[name] => Ram
[id] => 1
[date] => 12/04/2017 05:31:57
)
[1] => Array
(
[name] => Rahim
[id] => 5
[date] => 12/03/2017 12:31:57
)
[2] => Array
(
[name] => Raj
[id] => 4
[date] => 12/04/2017 17:31:57
)
)
答案 4 :(得分:0)
试试这个。这是一个简单易懂的例子。 substr
函数用于将字符串的一部分从0
开始到length - 2
以排除AM/PM
foreach($datArr as $key => $value){
$datArr[$key]['date'] = substr($value['date'], 0, strlen($value['date'])-2);
}
print_r($datArr);
快乐编码: - )
答案 5 :(得分:0)
您可以使用正则表达式通过循环删除不需要的单词。
foreach($datArr as &$i)
{
$i['date']=preg_replace('[AM|PM]',"",$i['date']);
}
print_r ($datArr);
输出
Array
(
[0] => Array
(
[name] => Ram
[id] => 1
[date] => 12/04/2017 05:31:57
)
[1] => Array
(
[name] => Rahim
[id] => 5
[date] => 12/03/2017 12:31:57
)
[2] => Array
(
[name] => Raj
[id] => 4
[date] => 12/04/2017 05:31:57
)
)
答案 6 :(得分:0)
<?php
$datArr=array(array("name"=>"Ram","id"=>1,"date"=>"12/04/2017 05:31:57 AM"),array("name"=>"Rahim","id"=>5,"date"=>"12/03/2017 12:31:57 PM"),array("name"=>"Raj","id"=>4,"date"=>"12/04/2017 05:31:57 PM"));
echo "<pre>";
print_r($datArr);
$a = array();
foreach ($datArr as &$str) {
$str = str_replace('PM', ' ', $str);
$str = str_replace('AM', ' ', $str);
$a[]=$str;
}
print_r($a);
?>
答案 7 :(得分:0)
只是说我的作品,如果您只是使用字符串函数直接删除AM / PM,我认为您可能会有错误的日期时间。如果你这样做,你将如何区分5AM到5PM?现在你将得到错误的数据。
如果您在5中取出PM,那么您需要将其格式化为军事时间(17)。要做到这一点,您需要先每次转换。您可以使用DateTime
:
$datArr = array_map(function($e){
$dt = new DateTime($e['date']);
$e['date'] = $dt->format('m/d/Y H:i:s'); // convert me into correct time
return $e;
}, $datArr);