You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:59:30