如何用PHP根据登录时间从指定数组中获取登录用户名?
处理登录记录数组提取指定格式结果
嘿,这事儿好办!我来帮你把这个PHP登录记录数组转换成你要的格式~
首先先把你的原始数组整理成规范的PHP代码格式:
$loginRecords = [ 'cena' => [ 'CentaurOne' => ['29/12/17 01:02:15', '13/01/18 09:23:21'], 'count' => 2 ], 'Bhumi' => [ 'CentaurOne' => ['29/12/17 01:21:30', '29/12/17 09:23:11'], 'count' => 2 ], 'John' => [ 'CentaurOne' => ['16/01/18 09:24:01', '18/01/18 02:51:20', '18/01/18 01:02:04', '19/01/18 01:02:04'], 'count' => 3 ] ];
根据你给出的示例需求(提取cena的第一条登录记录、John的第三和第四条记录),我写了一段PHP代码来处理:
// 定义要提取的用户及对应的记录索引(PHP数组索引从0开始) $targetExtract = [ 'cena' => [0], // 取cena的第1条记录 'John' => [2, 3] // 取John的第3、4条记录 ]; $outputParts = []; foreach ($targetExtract as $username => $recordIndices) { // 先拼接用户名 $part = $username; // 收集该用户的指定登录时间 $selectedTimes = []; foreach ($recordIndices as $idx) { if (isset($loginRecords[$username]['CentaurOne'][$idx])) { $selectedTimes[] = $loginRecords[$username]['CentaurOne'][$idx]; } } // 把时间拼到用户名后面,多个时间用" ,"分隔 $part .= ' ' . implode(' ,', $selectedTimes); $outputParts[] = $part; } // 把所有部分拼接成最终结果 $finalResult = implode(' ', $outputParts); echo $finalResult;
运行这段代码后,就能得到你想要的结果:cena 29/12/17 01:02:15 John 18/01/18 01:02:04 ,19/01/18 01:02:04
如果之后需要提取其他用户或不同的记录,只要修改$targetExtract数组里的内容就可以啦,比如要提取Bhumi的全部两条记录,就添加'Bhumi' => [0, 1]进去就行。
内容的提问来源于stack exchange,提问作者Shivu
相关产品推荐
相关产品推荐

