This question already has answers here:
How to count array with group by
(2 answers)
Closed 4 months ago.
I have 2 log files which i read and get the contents as array.
The array which i get is like this:
2 => array:9 [▼
"app" => "a.log"
"context" => "local"
"level" => "error"
"level_class" => "danger"
"level_img" => "exclamation-triangle"
"date" => "2018-10-25 21:01:04"
"text" => "Class 'Arcanedev\Support\Collection' not found
]
3 => array:9 [▼
"app" => "b.log"
"context" => "local"
"level" => "error"
"level_class" => "danger"
"level_img" => "exclamation-triangle"
"date" => "2018-10-26 16:49:07"
I need to get the count of all the error levels for each file.
For example:
a.log => "count of error level for a.log",
b.log => "count of error levels for b.log"
I have tried:
foreach($names as $filename){
$logs = $this->getLogs($this->filepath.$filename);
$result = array();
$result[ $filename ] = $logs;
foreach($logs as $log){
//$result[ $log['level'] ][] = $log;
$result[ $filename ][] = $log;
$counts = array_map('count', $result);
}
}
which gives me this result:
array:1 [▼
"a.log" => 20
]
Any help is appreciated.
Thank you in advance. :)
i solved the issue myself. I was tried another approach and it seemed to work.
Here is the solution:
foreach($names as $filename){
$logs = $this->getLogs($this->filepath.$filename);
$result = array();
foreach($logs as $log){
$result[ $log['app'] ][] = $log;
$counts = array_map('count', $result);
}
}
and the result:
array:2 [▼
"b-laravel.log" => 3
"laravel.log" => 7
]
Thank You.
Something like
print_r(array_count_values(array_column($array, 'app')));
I would test it but you didn't provide an array i can use. In theory it should work.
Related
This question already has answers here:
Merge the rows of two arrays (appending row data from one array to a row in another array)
(4 answers)
Closed 2 months ago.
I have two arrays:
$array1 =
[
0 =>
[
'data1' => value1
],
1 =>
[
'data1' => value2
]
];
$array2 =
[
0 =>
[
'data2' => value1
],
1 =>
[
'data2' => value2
]
];
Only i want create this:
$arrayFinish =
[
0 =>
[
'data1' => value1,
'data2' => value1
],
1 =>
[
'data1' => value2
'data2' => value2
]
];
I have done this:
foreach ($data1 as $key=>$val)
{
$arrayFinish[] =
[
$val,
];
foreach ($data2[$key] as $key2=>$val2)
{
array_push($arrayFinish[$key],['data2'=>$val2]);
}
}
My actually result:
array:2 [▼
0 => array:2 [▼
0 => {#1546 ▼
+"data1": "BWQHCLJCH"
}
1 => {#1547 ▼
+"data2": "00308F000825"
}
]
1 => array:2 [▼
0 => {#1548 ▼
+"data1": "OTGSAVJIU"
}
1 => {#1549 ▼
+"data2": "00308F000946"
}
]
]
I am trying to do it using PHP. I am blocked right now, i am sure that that the solution is using two loops, but i am doing any bad. If you can see my actually result is not similar to my wish result.
thank u for the help.
Thank u to all that tried to help me. I find a solution.
I am tried to clear the array
foreach ($array1 as $key=>$val)
{
$arrayFinish[$key] = $val;
foreach ($array2[$key] as $key2=>$val2)
{
array_push($arrayFinish[$key],["data2"=>$val2]);
}
}
Using a for cycle i can have access using one iteration to the information. This is not the great solution that i tried to find but working.
Best regards
if your both arrays would have same no of indexes then you can do using below code
foreach ($array1 as $key=>$val) {
$arrayFinish [] = [ $array1[$key], $array2[$key] ];
}
I'm pulling my hair on a proble which seems very simple, but I can't find a solution.
I have a simple $row array here :
array:2 [▼
"reference" => "ABCDEF"
"quantity" => "10"
]
I'm trying to parse it and retrieve quantities per reference using :
$line = [
'ref' => strtoupper($row["reference"]),
'quantity' => $row["quantity"]
];
I'm looping through array of lines using this code:
foreach ($rows as $row) {
$line = [
'ref' => strtoupper($row['reference']),
'quantity' => $row['quantity']
];
}
As a test, my main array $rows has 2 lines :
^ array:3 [▼
0 => array:2 [▼
0 => "ABCDEF"
1 => "10"
]
1 => array:2 [▼
0 => "WXCVBN"
1 => "3"
]
2 => array:1 [▼
0 => null
]
]
However, I'm getting the following error :
Undefined index: reference
Strangely enough, if I comment out the
'ref' => strtoupper($row["reference"]),
line, I can get the "quantity" value with no issue...
I know the key is in there, since the debug of the $row object gives the above result.
It must be dead simple... but I can't find the solution...
If anyone could please help ?
So apparently the $row variable a row from a bigger array that is used in a foreach loop.
This might be the solution to your problem.
$array = [
[ "reference" => "ABC", "quantity" => "10"],
[ "reference" => "ABC", "quantity" => "10"],
[ "reference" => "ABC", "quantity" => "10"],
];
$line[] = '';
foreach($array as $row)
{
$line['ref'] = $row['reference'];
$line['quantity'] = $row['quantity'];
}
In this example $array is your bigger array, i use it to test the example.
After that i create a empty array $line to append the "new" data.
Could you try this?
Edit:
After taking a look at your loop and the array i noticed that your array doesnt have a reference key. Can you try strtoupper(row[0])?
Viewing your code seems that you convert $row array to $line without rewrite key in new array.
your code
foreach ($rows as $row) {
$line = [
'ref' => strtoupper($row['reference']),
'quantity' => $row['quantity']
];
}
With your rewrite you can access $line data by index, not by key
array:3 [▼
0 => array:2 [▼
0 => "ABCDEF"
1 => "10"
]
...
]
My solution
If you want to access your $line data by key, you need to rewrite your loop as:
foreach($rows as $rowData) {
foreach($rowData as $rowKey => $rowValue) {
$data = [
'ref' => $rowKey => strtoupper$($rowValue),
'quantity' => $row['quantity']
];
}
$line[] = $data;
}
This question already has answers here:
php multi-dimensional array remove duplicate [duplicate]
(6 answers)
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
php remove duplicates from multidimensional array by value [duplicate]
(3 answers)
Closed 3 years ago.
I am in a confusing situation, I have an array where I want the duplicates removed. e.g
$arr = [
0 => array:25 [
"content" => "abc" //duplicate
]
1 => array:25 [
"content" => "def"
]
2 => array:1 [
"content" => "abc" //duplicate
]
]
Now, I use array filter like this trying to remove duplicate "abc"
$filtered = array_filter( $arr, function($item) use($arr)
{
foreach($arr as $item1)
{
return $item1['content'] !== $item['content']
}
}
This will give me the $filtered variable to be
[
0 => array:25 [
"content" => "def"
]
]
But, what I really want is
[
0 => array:25 [
"content" => "abc"
]
1 => array:25 [
"content" => "def"
]
]
i.e Removing one instance of duplicate not all.
You should somehow keep track of already visited $item['content']:
$visited = [];
$filtered = array_filter(
$arr,
function($item) use(&$visited) {
if (!isset($visited[$item['content']])) {
$visited[$item['content']] = 1;
return true;
}
return false;
}
);
I have a array list, How can I get with PHP the highest key value pair in an array?
array:4 [
0 => array:2 [
"free_shipping" => true
"coupon_amt" => 20
]
1 => array:2 [
"coupon_amt" => 120
"free_shipping" => false
]
2 => array:2 [
"free_shipping" => false
"coupon_amt" => 100
]
3 => array:2 [
"coupon_amt" => 200
"free_shipping" => true
]
]
Any ideas or suggestions on how I can or should do this?
array:4 [
0 => array:2 [
"free_shipping" => true
"coupon_amt" => 200
]
]
A simple loop seems the best option to me.
$max = null;
foreach ($array as $part) {
if ($max === null || $part['coupon_amt'] > $max['coupon_amt']) {
$max = $part;
}
}
Could you try this:
First Convert the array to collection
$collection = collect($array);
And then sort it
$sorted = $collection->sortByDesc('coupon_amt');
then if you want it to be array again just do this
$array = $sorted->toArray();
then just get the first array
$first = $array[0];
Hope it helps!
Use the collections' available methods such as max and where.
<?php
$collection = collect($array);
print_r($collection->where('coupon_amt',$collection->max('coupon_amt'))->all());
$collection = collect($array);
print_r($collection->where('coupon_amt',$collection->max('coupon_amt'))->all());
"foreach" loop results in an 'Undefined Index' error when I manipulate an array.
The array is generated from MS Excel and works fine with PHP array functions. When I use "foreach" loop to extract data, I get the 'Undefined Index' error but when I dump the value of the index, I get desired results.
The code extract is as below:
foreach ($membership_data as $data){
//dd($data["Gender"]);
if($data["Gender"]=='Male') {
$males_dates_of_birth[] = $data['DateofBirth'];
} elseif ($data["Gender"]=='Female'){
$females_dates_of_birth[] = $data['DateofBirth'];
} else {
$erroneous_gender_dates_of_birth[] = $data['DateofBirth'];
}
}
How can this be solved?
Or else, is there a better way to get three arrays containing dates of birth for various genders? The sample data array is as below.
array:100 [▼
6 => array:15 [▼
"MemberNumber" => "48490"
"Surname" => "Wuckert"
"FirstName" => "Clement"
"OtherNames" => "Dr. Monique Murazik II"
"DateofBirth" => "1968-08-4"
"DateEmployed" => "1988-08-04"
"DateJoinedScheme" => "1968-08-4"
"Gender" => "Female"
"DocumentType" => "National ID"
"DocumentNumber" => "a370523307a"
"KRAPIN" => "a459831176c"
"NSSFNumber" => "a526026924k"
"TelephoneNumber" => "722136702"
"EmailAddress" => "annie99#yahoo.com"
"TelephoneNumberCountry" => "Kenya"
]
7 => array:15 [▶]
8 => array:15 [▶]
9 => array:15 [▶]
10 => array:15 [▶]
11 => array:14 [▶]
You need to check if the key is existing in the array or not using array_key_exists
if(array_key_exists('Gender', $data) && $data["Gender"]=='Male') {
$males_dates_of_birth[] = array_key_exists('DateofBirth', $data) ? $data['DateofBirth'] : '';
} elseif (array_key_exists('Gender', $data) && $data["Gender"]=='Female'){
$females_dates_of_birth[] = array_key_exists('DateofBirth', $data) ? $data['DateofBirth'] : '';
} else {
$erroneous_gender_dates_of_birth[] = array_key_exists('DateofBirth', $data) ? $data['DateofBirth'] : '';
}