How to get a user log failed details in log file - php

In a log file I want get a how many times user is log failed and the date details I have code for this but its not working correctly.
This is a code
$file = new SplFileObject("failed.log", "r");
while(!$file->eof())
{
$data[] = current (($file->fgetcsv("|"))) . next (($file->fgetcsv("|")));
}
$abc = implode(",", $data);
echo $abc;
$br = "\n";
In this code I am getting like this
John26:Dec:2017:16:15:27,Abc29:Dec:2017:09:39:45,Bhumi29/Dec/2017 14:15:41,Abc
Its not correct, it read first line name and and second line date I don't no what is wrong in my code.
This is my log failed details
John|26:Dec:2017:16:09:45|192.168.0.101|CentaurOne|Failed
cena|26:Dec:2017:16:15:27|192.168.0.101|CentaurOne|Failed
Abc|27:Dec:2017:09:25:25|192.168.0.101|CentaurOne|Failed
Bhumi|29:Dec:2017:09:39:45|192.168.0.101|CentaurOne|Failed
Bhumi|29:Dec:2017:10:19:28|192.168.0.101|CentaurOne|Failed
Avccc|29/Dec/2017 14:15:41|192.168.0.101|CentaurOne|Failed
Abc|04/Jan/2018 15:54:35|192.168.0.101|CentaurOne|Failed

As suggested in the comments, you should rethink saving failed logins to a text log especially if you intend to make this a routine action. As the file grows, it will become memory intensive:
# Saves contents of file into array by end of lines
$explode = file("failed.log",FILE_SKIP_EMPTY_LINES);
# Loop rows
foreach($explode as $row) {
# Explode the | character
$exp = explode('|',$row);
# Save the user to the an array
$new[$exp[0]]['dates'][] = $exp[1];
# Count how many times tried
if(!isset($new[$exp[0]]['count']))
$new[$exp[0]]['count'] = 1;
else
$new[$exp[0]]['count'] += 1;
}
print_r($new);
Should give you:
Array
(
[John] => Array
(
[dates] => Array
(
[0] => 26:Dec:2017:16:09:45
)
[count] => 1
)
[cena] => Array
(
[dates] => Array
(
[0] => 26:Dec:2017:16:15:27
)
[count] => 1
)
[Abc] => Array
(
[dates] => Array
(
[0] => 27:Dec:2017:09:25:25
[1] => 04/Jan/2018 15:54:35
)
[count] => 2
)
[Bhumi] => Array
(
[dates] => Array
(
[0] => 29:Dec:2017:09:39:45
[1] => 29:Dec:2017:10:19:28
)
[count] => 2
)
[Avccc] => Array
(
[dates] => Array
(
[0] => 29/Dec/2017 14:15:41
)
[count] => 1
)
)
Use print_r() to see how to access array key/values.

Related

Adding an increasing number into a php array (so i can later call it with a php echo)

With this code
$files = scandir('audio');
$files = array_slice($files, 2);
$files = array_combine(range(1, count($files)), $files);
echo "<pre>";
print_r($files);
echo "</pre>";
I am able to scan a dir and list its files in an array like so..
Array
(
[1] => audio1.mp3
[2] => audio2.mp3
[3] => audio3.mp3
)
What I'd like to do is insert an increasing value number for each file it finds that I can later reference so the array becomes
Array
(
[1] => Array
(
[0] => 1
[1] => audio1.mp3
)
[2] => Array
(
[0] => 2
[1] => audio2.mp3
)
[3] => Array
(
[0] => 3
[1] => audio3.mp3
)
)
I haven't a clue where to start with this one so can't say what I've tried, any help appreciated.
PS: this is in a bid for me to solve the issue I'm having in this question Problem when using echo'd ++$value in javascript as part of a html5 audio player page because if these values are in the array I won't have to increase them with ++ on the echo side.
foreach($files as $key=>$f) {
$files[$key] = array($key, $f);
}

Printing out an array bit of a different way

I've been struggling quite a bit with this. I have a folder with a list of files and a basic naming order - abc-def-xyz.php
I'm trying to create an index page to list all of the files automatically in groups. So:
abc-def-xyz.php
abc-def-xzz.php
abb-def-xyz.php
Would show in the php index page as:
abc
def
xyz
xzz
abb
def
xyz
I can get to the point of exploding the file names and removing the extension, then i'm just lost. Any advice would be much appreciated. Thank you!
Array
(
[0] => Array (this is applyOnline-alert-warning.php)
(
[0] => applyOnline
[1] => alert
[2] => warning
)
[1] => Array
(
[0] => applyOnline
[1] => collectionOrDelivery
)
)
Using the methodology from this answer How to write getter/setter to access multi-level array by key names?:
$array = array();
foreach($files as $file) {
$temp = &$array;
foreach(explode('-', $file) as $key) {
$temp =& $temp[pathinfo($key, PATHINFO_FILENAME)];
}
$temp = $file;
}
Yields an array where the key names can be used:
Array
(
[abc] => Array
(
[def] => Array
(
[xyz] => abc-def-xyz.php
[xzz] => abc-def-xzz.php
)
)
[abb] => Array
(
[def] => Array
(
[xyz] => abb-def-xyz.php
)
)
)

PHP Nested foreach with Key

I have been trying to work this out for two days now and am hitting a brick wall. I have a skyscanner array that has flight itineraries where I have the flight
Leg - being Itineraries -> OutboundLegId -
and also the legs which shows the flight number - being
Legs -> FlightNumbers -> FlightNumber.
What I am trying to achieve is to display the Itinerary and then join the Flight Number on to that. But no matter what I try I cannot get this to work. I have read all about Keys on here and tried loads of examples but am coming up with nothing. Can someone point me in the right direction please?
Example arrays below
[Itineraries] => Array
(
[0] => Array
(
[OutboundLegId] => 13542-1610140610-29-0-13445-1610141240
[InboundLegId] => 13445-1610211340-29-0-13542-1610211640
[PricingOptions] => Array
(
[0] => Array
(
[Agents] => Array
(
[0] => 2174187
)
[QuoteAgeInMinutes] => 31
[Price] => 200.98
[DeeplinkUrl] => http://partners.api.skyscanner.net/apiservices/deeplink/v2?_cje=5JlLCgyPUKY0hT8T0Ybh6dL0Xf0htAiHTFX7RU79eeI3XvrsxvEqP1QUJAoHiHRd&url=http%3a%2f%2fwww.apideeplink.com%2ftransport_deeplink%2f4.0%2fUK%2fen-gb%2fGBP%2fcook%2f2%2f13542.13445.2016-10-14%2c13445.13542.2016-10-21%2fair%2fairli%2fflights%3fitinerary%3dflight%7c-32294%7c1152%7c13542%7c2016-10-14T06%3a10%7c13445%7c2016-10-14T12%3a40%2cflight%7c-32294%7c1153%7c13445%7c2016-10-21T13%3a40%7c13542%7c2016-10-21T16%3a40%26carriers%3d-32294%26passengers%3d1%2c0%2c0%26channel%3ddataapi%26cabin_class%3deconomy%26facilitated%3dfalse%26ticket_price%3d200.98%26is_npt%3dfalse%26is_multipart%3dfalse%26client_id%3dskyscanner_b2b%26request_id%3d3bc96bda-fd7c-403a-b841-2ccc3c26071d%26commercial_filters%3dfalse%26q_datetime_utc%3d2016-09-29T08%3a18%3a27
)
[Legs] => Array
(
[0] => Array
(
[Id] => 13542-1610140610-29-0-13445-1610141240
[SegmentIds] => Array
(
[0] => 1
)
[OriginStation] => 13542
[DestinationStation] => 13445
[Departure] => 2016-10-14T06:10:00
[Arrival] => 2016-10-14T12:40:00
[Duration] => 270
[JourneyMode] => Flight
[Stops] => Array
(
)
[Carriers] => Array
(
[0] => 105
)
[OperatingCarriers] => Array
(
[0] => 105
)
[Directionality] => Outbound
[FlightNumbers] => Array
(
[0] => Array
(
[FlightNumber] => 1152
[CarrierId] => 105
)
)
)
Assuming this is one big array and its called $data you can nest a couple of foreach loops.
I use foreach loops as I assume there are cases where this data structure get more complex than the one you show
foreach ( $data['Itineraries'] as $itin ) {
foreach ( $data['Legs'] as $legs) {
if ($legs['Id'] == $itin['OutboundLegId']) {
// we matched the itinerary with a leg
echo $legs['OutboundLegId'] . ' ' . $legs['FlightNumbers'][0]['FlightNumber'];
}
}
}
Use it as draft. Can't perform function without feedback.
Put proper arrays instead of {YOUR-ARRAY-WITH-LEGS} and {YOUR-ARRAY-WITH-ITINERARIES}
$sortedLegs = array_column('Id', {YOUR-ARRAY-WITH-LEGS});
$joinedArray = array_map(function($itinerary) use($sortedLegs){
if(array_key_exists($itinerary['OutboundLegId'],$sortedLegs)) {
$itinerary['legs'] = $sortedLegs[$itinerary['OutboundLegId']];
}
return $itinerary;
},{YOUR-ARRAY-WITH-ITINERARIES});

PHP Split array in subarrays

Note, array_chunk is not my solution (It seems to me).
I have an array of about 150.000 elements
Array
(
[0] => Array
(
[name] => Danilo
[phone] => 33568
)
[1] => Array
(
[name] => Alessandro
[phone] => 392222
)
[2] => Array
(
[name] => Alex
[phone] => 3922
)
[3] => Array
(
[name] => Capa
[phone] => 392
)
)
And so on. I would split this array in several arrays, of (for example) 3.000 elements every one.
I saw array_chunk, but it returns a single array with several subarray.
I need several subarray to store them in a database and elaborate in future.
I'm getting crazy to write a snippet starting from that $temp and divide it into smaller array.
$size_chunks = 1;
$temp = array_chunk($recipients, $size_chunks);
foreach ($temp as $key=>$value)
{
if ($key<$size_chunks)
{
$to_store[] = $temp[$key];
}
//print_r($to_store);
// pseudo sql
// INSERT INTO table (sub_recipient) VALUES ($to_store);
$to_store = array();
}
So, every time that for loop end, reduce temp, store $to_store array and restart for others chunks.
Thank you very much.
PS in my example chunk==1 because starting array is small... ;)
With my example of chunk = 1, I need from starting array this 4 arrays:
Array
(
[0] => Array
(
[name] => Danilo
[phone] => 33568
)
)
Array
(
[0] => Array
(
[name] => Alessandro
[phone] => 39222
)
)
Array
(
[0] => Array
(
[name] => Alex
[phone] => 39222
)
)
Array
(
[0] => Array
(
[name] => Capa
[phone] => 392
)
)
Another explain
1 - With a starting array of 15.000 elements, and chunk of 3.000, I need in output (15.000 / 3.000) = 5 arrays. I will save them in database, so in DB I will have 5 rows (a row for every array).
2 - With a starting array of 4 elements, and chunk of 1, I need in output (4 / 1) = 4 arrays. I will save them in database, so in DB I will have 4 rows (a row for every array).
array_chunks() already does what you want, you just have to save it:
$chunks = array_chunk($array, $size_chunks);
foreach ($chunks as $chunk) {
// save $chunk to your database
}
$recipients = Array(
Array("fdbvfdb","dsacsdcds"),
Array("hrloo","dacdsc"),
Array("dcsdc","adcsd"),
Array("dcsdc","adcsd")
);
$total = count($recipients);//count 150.000 elements
$i=1;
for($i=0;$i<$total;$i++){
$O = array_slice($recipients,$i,1);
print_r($O);
//Your insert/Save code
}
you can use this code there is uses Array_Slice

Merge arrays together based on different values

I am having trouble thinking of the logic with the following problem:
I have the following array (has been snipped, as its much larger)
Array
(
[0] => Array
(
[code] => LAD001
[whqc] => GEN
[stocktag] => NONE
[qty] => 54
)
[1] => Array
(
[code] => LAD001
[whqc] => GEN
[stocktag] => NONE
[qty] => 6
)
[2] => Array
(
[code] => LAD004
[whqc] => HOLD
[stocktag] => NONE
[qty] => 6
)
)
I basically need to comebine all the keys in this array so that where the code, whqc and stocktag are the same, add the qty values together. With the example below, I need to end up with this:
Array
(
[0] => Array
(
[code] => LAD001
[whqc] => GEN
[stocktag] => NONE
[qty] => 60
)
[1] => Array
(
[code] => LAD004
[whqc] => HOLD
[stocktag] => NONE
[qty] => 6
)
)
As the first and second keys of the array had the same code, whqc and stocktag, the qty's have been added together into the one key.
Any ideas?
I would suggest combining the group values in to a hash, storing the full array under the hash as a key and if you have a duplicate, add the quantity, then do array_values() to pull the results.
$aggregated = array();
foreach ($records as $cRec) {
// The separator | has been used, if that appears in the tags, change it
$cKey = md5($cRec['code'] . '|' . $cRec['whqc'] . '|' . $cRec['stocktag']);
if (array_key_exists($cKey, $aggregated)) {
$aggregated[$cKey]['qty'] += $cRec['qty'];
} else {
$aggregated[$cKey] = $cRec;
}
}
// Reset the keys to numerics
$aggregated = array_values($aggregated);
I would try something like:
$output = array();
foreach($array as $details){
//make distinct key
$key = $details['code'].'_'.$details['whqc'];
if(!isset($output[$key])){
$output[$key] = $details;
}else{
$output[$key]['qty'] += $details['qty'];
$output[$key]['stocktag'] = $details['stocktag'];
}
}
$output = array_values($output);
print_r($output);
update: Orbling was first ;-)

Categories