Php 我怎么才能把这个大的双人沙发调对呢?
我在另一个房间里有一个房间。这个foreach的原因是我想得到一个多个数组,其中包含一些我已经得到的IP和来自IP的所有日期。。。这有点复杂。。我需要将日期格式化为: Y-m-d,H:m:s 这是可行的,但是多重变量的最终输出并不像我预期的那样 foreach第一部分的输出如下:Php 我怎么才能把这个大的双人沙发调对呢?,php,arrays,foreach,Php,Arrays,Foreach,我在另一个房间里有一个房间。这个foreach的原因是我想得到一个多个数组,其中包含一些我已经得到的IP和来自IP的所有日期。。。这有点复杂。。我需要将日期格式化为: Y-m-d,H:m:s 这是可行的,但是多重变量的最终输出并不像我预期的那样 foreach第一部分的输出如下: Array ( [0] => Array ( ) [1] => Array (
Array
(
[0] => Array
(
)
[1] => Array
(
[ip] => 72.xx.xx.xx
[all_dates] => Array
(
[0] => 12/Oct/2015:00:30:15
[1] => 12/Oct/2015:00:30:24
[2] => 12/Oct/2015:00:30:49
[3] => 12/Oct/2015:00:30:57
[4] => 12/Oct/2015:00:30:57
[2] => Array
(
[ip] => 85.xx.xxx.xx
[all_dates] => Array
(
[0] => 12/Oct/2015:00:32:19
[1] => 12/Oct/2015:00:32:33
[2] => 12/Oct/2015:00:32:33
[3] => 12/Oct/2015:00:34:30
[4] => 12/Oct/2015:00:38:59
[5] => 12/Oct/2015:00:39:20
)
)
$ip_with_date[] = [];
首先。。我不知道为什么[0]=>数组是空的,或者更好,为什么[1]=>数组不在[0]位置
第二件事是我需要把所有的日期都格式化。我在Second foreach循环中这样做了,但在此之后,乘法器没有得到正确的输出。嗯,它没有真正的输出
以下是我的代码更新:
$diff_ips = array_unique($ip_array);
$ip_with_date = [];
foreach ($diff_ips as $ip) {
$get_dates = shell_exec("grep $ip $path" . $inputs['domain'] . ".log | awk '{print $4}'");
$new_date = str_replace('[', '', $get_dates);
$array_date = explode("\n", $new_date);
array_pop($array_date);
foreach ($array_date as &$dates) {
$dates[11] = ' ';
$dates = date('Y-m-d, H:m:s', strtotime(str_replace('/', '-', $dates)));
}
}
$ip_with_date[] = [
'ip' => $ip,
'all_dates' => $array_date
];
echo "<pre>";
print_r($ip_with_date);
echo "<pre>";
第一个foreach循环结束=第二个循环开始的地方,给出上面看到的输出
但是现在日期安排得不好。这就是为什么我有第二个foreach循环。
$diff_ips = array_unique($ip_array);
$ip_with_date = [];
foreach ($diff_ips as $ip) {
$get_dates = shell_exec("grep $ip $path" . $inputs['domain'] . ".log | awk '{print $4}'");
$new_date = str_replace('[', '', $get_dates);
$array_date = explode("\n", $new_date);
array_pop($array_date);
foreach ($array_date as &$dates) {
$dates[11] = ' ';
$dates = date('Y-m-d H:m:s', strtotime(str_replace('/', '-', $dates)));
}
$ip_with_date[] = [
'ip' => $ip,
'all_dates' => $array_date
];
}
行,是因为行后11有问题。性格这就是为什么我把它从“:”设置为“
在我定义$formatted_date变量时,日期看起来很完美。当然,它不会覆盖多数组中的日期,这是我的问题
我真的尽了最大努力,但还没有找到解决办法。末尾的输出应如下所示:
Array
(
[0] => Array
(
)
[1] => Array
(
[ip] => 72.xx.xx.xx
[all_dates] => Array
(
[0] => 12/Oct/2015:00:30:15
[1] => 12/Oct/2015:00:30:24
[2] => 12/Oct/2015:00:30:49
[3] => 12/Oct/2015:00:30:57
[4] => 12/Oct/2015:00:30:57
[2] => Array
(
[ip] => 85.xx.xxx.xx
[all_dates] => Array
(
[0] => 12/Oct/2015:00:32:19
[1] => 12/Oct/2015:00:32:33
[2] => 12/Oct/2015:00:32:33
[3] => 12/Oct/2015:00:34:30
[4] => 12/Oct/2015:00:38:59
[5] => 12/Oct/2015:00:39:20
)
)
$ip_with_date[] = [];
正如我所说,这有点复杂
解决了。
工作代码:
$ip_with_date = []; // no [] here
第一个问题:
定义
foreach ($array_date as &$dates) {
// see this &? This means that changing $dates will affect item in $array_date
$dates[11] = ' ';
$dates = date('Y-m-d, H:m:s', strtotime(str_replace('/', '-', $dates)));
}
// Only after array_dates processing - add array to $ip_with_date
$ip_with_date[] = [
'ip' => $ip,
'all_dates' => $array_date
];
平均值-将带有日期的$ip_视为数组,并向其中添加空数组。这是键0处的空数组
替换为:
$diff_ips = array_unique($ip_array);
$ip_with_date = [];
foreach ($diff_ips as $ip) {
$get_dates = shell_exec("grep $ip $path" . $inputs['domain'] . ".log | awk '{print $4}'");
$new_date = str_replace('[', '', $get_dates);
$array_date = explode("\n", $new_date);
array_pop($array_date);
foreach ($array_date as $dates) {
$formed_date[] = date('Y-m-d, H:m:s', strtotime(str_replace('/', '-', $dates)));
}
$ip_with_date[] = [
'ip' => $ip,
'all_dates' => $formed_date
];
}
第二个问题:
所以你有$format\u date。你期待什么?它只是一个变量,你什么都不做。以某种方式分配它或做某事。但我建议你:
试试下面的代码
$ip_,_日期=[];嗯,那没什么帮助..谢谢你:第一部分很好用:。第二秒没有改变任何东西:/输出仍然是一样的。我用我当前的代码更新了我的问题好吧,这工作得更好,但现在它只给了我最后一个数组键。。。不是每个IP都有日期,只有最后一个IP和他的日期。。我用新代码和新输出更新了我的问题,为什么会出现“,”呢?您了解代码中发生了什么吗?为什么会出现“,”-你看到日期“Y-m-d,H:m:s”了吗?里面有个逗号。哇!为什么在foreach结束后,将项目添加到带有日期[]的$ip_中?嗯。。这是我的错。。我没有看到这个“,”:/但这并没有改变这样一个事实,即它只显示了最后一个数组键,而不是所有的。输出与我想要的一样,但输出中的日期不是真实的日期。。他们只是每次:[0]=>1970-01-01,00:01:00尝试strotime$dates而不是这个strotimestr_替换“/”,“-”,$datesu_mulder在这方面帮了我大忙:我的代码现在运行良好:无论如何,谢谢:
$diff_ips = array_unique($ip_array);
$ip_with_date = [];
foreach ($diff_ips as $ip) {
$get_dates = shell_exec("grep $ip $path" . $inputs['domain'] . ".log | awk '{print $4}'");
$new_date = str_replace('[', '', $get_dates);
$array_date = explode("\n", $new_date);
array_pop($array_date);
foreach ($array_date as $dates) {
$formed_date[] = date('Y-m-d, H:m:s', strtotime(str_replace('/', '-', $dates)));
}
$ip_with_date[] = [
'ip' => $ip,
'all_dates' => $formed_date
];
}