Click here to Skip to main content
15,920,053 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
Posted
Updated 7-Jan-18 23:44pm
Comments
EZW 9-Jan-18 1:41am    
I would change the format of the array.

Something like

array(
    'name'      => 'John',
    'Something' => 'CentaurOne'
    'Dates'     => array(
                       0 => '26:Dec:2017:16:09:45'
                       )
)
EZW 9-Jan-18 1:45am    
In your case that should look something like:

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

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

    $key++;
}
shivushrami 10-Jan-18 1:10am    
Thanks It's working .
I want username under date details BhumiCentaurOne29:Dec:2017:09:39:45,29:Dec:2017:10:19:28=2

AvcccCentaurOne29/Dec/2017 14:15:41,04/Jan/2018 15:54:35=2 like this is it possibble

1 solution

You may use print_r[^] method of PHP...
PHP
fwrite($your_file, print_r($your_array, true));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900