如何使用PHP将数组转换为文本文件 [英] How to convert array into text file using PHP

查看:57
本文介绍了如何使用PHP将数组转换为文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是日志的输出失败它是一个数组我想要像这样的文本行

// John CentaurOne 26:Dec:2017:16:09:45 count => ; 1

// cena CentaurOne 26:Dec:2017:16:15:27 count => 1

// Bhumi CentaurOne 29:Dec:2017:09:39:45 29:Dec:2017:10:19:28 count => 2

如何转换成这个请帮帮我。我是php的新人



数组



[John] =>数组


[CentaurOne] =>数组



[0] => 26:Dec:2017: 16:09:45





[count] => 1





[cena] =>数组



[CentaurOne] =>数组



[0] => 26:Dec:2017:16:15:27





[count] => 1





[abc] =>数组



[CentaurOne] =>数组



[0] => 27:Dec:2017:09:25:25

[1] => 04 / Jan / 2018 15:54:35





[count ] => 2





[Bhumi] =>数组



[CentaurOne] =>数组



[0] => 29:Dec:2017:09:39:45

[1] => 29:Dec:2017:10:19:28





[count] => ; 2





[Avccc] =>数组



[CentaurOne] =>数组



[0] => 29 / 12月14日14:15:41





[count] => 1

)< br $> b $ b





我尝试了什么:



这是我获取用户名,日期和系统名称的php代码



$ explode = file(failed.log, FILE_SKIP_EMPTY_LINES);







foreach($ $ as $ row)

{

#爆炸|字符

$ exp = explode('|',$ row);

#将用户保存到数组

$ new [$ exp [0]] [$ exp [3]] [] = $ exp [1];



#计算尝试了多少次

if(!isset($ new [$ exp [0]] ['count']))

$ new [$ exp [0]] ['count'] = 1;

else

$ new [$ exp [0]] ['count'] + = 1;









}





$ abc = print_r($ new,true);

echo $ abc;

解决方案

explode = file(failed.log,FILE_SKIP_EMPTY_LINES);







foreach(


explode as

row)

{

#爆炸|人物


This is the out put for log failed it's a an array i want to like a text line like this
//John CentaurOne 26:Dec:2017:16:09:45 count => 1
//cena CentaurOne 26:Dec:2017:16:15:27 count => 1
//Bhumi CentaurOne 29:Dec:2017:09:39:45 29:Dec:2017:10:19:28 count => 2
how to convert to this please help me. i am new one for php

Array
(
[John] => Array
(
[CentaurOne] => Array
(
[0] => 26:Dec:2017:16:09:45
)

[count] => 1
)

[cena] => Array
(
[CentaurOne] => Array
(
[0] => 26:Dec:2017:16:15:27
)

[count] => 1
)

[Abc] => Array
(
[CentaurOne] => Array
(
[0] => 27:Dec:2017:09:25:25
[1] => 04/Jan/2018 15:54:35
)

[count] => 2
)

[Bhumi] => Array
(
[CentaurOne] => Array
(
[0] => 29:Dec:2017:09:39:45
[1] => 29:Dec:2017:10:19:28
)

[count] => 2
)

[Avccc] => Array
(
[CentaurOne] => Array
(
[0] => 29/Dec/2017 14:15:41
)

[count] => 1
)

)

What I have tried:

This is my php code for getting user name, date and systemname

$explode = file("failed.log",FILE_SKIP_EMPTY_LINES);



foreach($explode as $row)
{
# Explode the | character
$exp = explode('|',$row);
# Save the user to the an array
$new[$exp[0]][$exp[3]][]= $exp[1];

# Count how many times tried
if(!isset($new[$exp[0]]['count']))
$new[$exp[0]]['count'] = 1;
else
$new[$exp[0]]['count'] += 1;




}


$abc = print_r($new,true);
echo $abc;

解决方案

explode = file("failed.log",FILE_SKIP_EMPTY_LINES);



foreach(


explode as


row)
{
# Explode the | character


这篇关于如何使用PHP将数组转换为文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆