作者:蓝色调调2502937087 | 来源:互联网 | 2023-09-03 17:00
我有详细信息数组:
[info_details] => Array
([0] => title: this is title[1] => name: this is name[2] => created this is date
)
我需要将此数组格式化为:
[info_details] => Array
([title] => this is title[name] => this is name[created] => this is date
)
那么爆炸粗体的最佳方法是什么?
我的代码现在:
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][] = preg_split('/]*>/', $value);
}
但这不起作用.
解决方法:
可以使用preg_match()和str_replace()尝试使用regex
$pattern = "/.+:<\/b>\s?/";
$arr['info_details'] = [
'title: this is title',
'name: this is name',
'created: this is date',
];
$new_arr['info_details'] = [];
foreach($arr['info_details'] as $val){
preg_match($pattern, $val, $m);
$new_arr['info_details'][trim(strip_tags($m[0]), ': ')] = str_replace($m[0], '', $val);
}
print '';
print_r($new_arr);
print '
';
输出量
Array
(
[info_details] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
)