How to group subarrays by a column value?
There is no native one, just use a loop.
$result = array();
foreach ($data as $element) {
$result[$element['id']][] = $element;
}
Grouping Associative Arrays
Inside function do foreach()
<?php
$associativeArray = array("Ripe Mango"=>"Yellow", "Strawberry"=>"Red", "Lemon"=>"Yellow");
function groupByColor($associativeArray){
$final_array = [];
foreach($associativeArray as $key=>$val){
$final_array[$val][] = $key;
}
return $final_array;
}
print_r(groupByColor($associativeArray));
Output:- https://eval.in/933011
Note:- you can assign groupByColor($associativeArray)
returned array to a new variable and print that variable like below:-
$color_array = groupByColor($associativeArray);
print_r($color_array);
grouping of array in PHP
Working Solution
<?php
$your_arr = array(
array('id' => 1,'qty' => 5),
array('id' => 2,'qty' => 5),
array('id' => 2222,'qty' => 5),
array('id' => 1,'qty' => 5),
array('id' => 3,'qty' => 5)
);
$new = array();
foreach ($your_array as $r){
if(!isset($new[$r['id']]))$t=0; //check the current id exist in $new if Not $t = 0;
else $t=$r['qty']; //if yes $t's value become the saved value in $new[$r['id']]
$new[$r['id']]['id'] = $r['id'];
$new[$r['id']]['qty'] = ($t+$r['qty']); // add the new value with $new[$r['id]]'s value.
}
echo "<pre>";print_r($new);
?>
Group array by values php
Try this :
// Create a new array
$result = array();
// Loop through your array
foreach ($array as $value) {
// Create a key that start at 0
$i = 0;
// Test if $result[0] exist : if yes, you have data, else you have nothing
if (isset($result[$i])) {
do {
// Check if the 'name' is new : if yes, $i++ to check next name
if ($result[$i]['name'] !== $value['name']) $i++;
// If you find similar name, stop here
else break;
} while (isset($result[$i]));
// Just add $result[0] with name and age value
} else {
$result[$i] = array (
'name' => $value['name'],
'age' => $value['age']
);
}
// Now you know the index of result you need to work with
// Just add a new code / group array to your code index
$result[$i][$value['group']][] = array($value['code'], $value['group']);
}
If you do var_dump($result);
the output is :
array (size=1)
0 =>
array (size=7)
'name' => string 'John Doe' (length=8)
'age' => string '36' (length=2)
1000 =>
array (size=1)
0 =>
array (size=2)
0 => string '437' (length=3)
1 => string '1000' (length=4)
7777 =>
array (size=2)
0 =>
array (size=2)
0 => string '437' (length=3)
1 => string '7777' (length=4)
1 =>
array (size=2)
0 => string '437' (length=3)
1 => string '7777' (length=4)
4000 =>
array (size=2)
0 =>
array (size=2)
0 => string '437' (length=3)
1 => string '4000' (length=4)
1 =>
array (size=2)
0 => string '437' (length=3)
1 => string '4000' (length=4)
5000 =>
array (size=1)
0 =>
array (size=2)
0 => string '437' (length=3)
1 => string '5000' (length=4)
6000 =>
array (size=2)
0 =>
array (size=2)
0 => string '437' (length=3)
1 => string '6000' (length=4)
1 =>
array (size=2)
0 => string '437' (length=3)
1 => string '6000' (length=4)
EDIT :
To get only group value equal to 7777
and 6000
replace
$result[$i][$value['group']][] = array($value['code'], $value['group']);
by
$group_value = $value['group'] == "7777" || $value['group'] == "6000" ? $value['group'] : "Others";
$result[$i][$group_value][] = array($value['code'], $value['group']);
Now the output of var_dump($result);
is :
array (size=1)
0 =>
array (size=5)
'name' => string 'John Doe' (length=8)
'age' => string '36' (length=2)
'Others' =>
array (size=4)
0 =>
array (size=2)
0 => string '437' (length=3)
1 => string '1000' (length=4)
1 =>
array (size=2)
0 => string '437' (length=3)
1 => string '4000' (length=4)
2 =>
array (size=2)
0 => string '437' (length=3)
1 => string '4000' (length=4)
3 =>
array (size=2)
0 => string '437' (length=3)
1 => string '5000' (length=4)
7777 =>
array (size=2)
0 =>
array (size=2)
0 => string '437' (length=3)
1 => string '7777' (length=4)
1 =>
array (size=2)
0 => string '437' (length=3)
1 => string '7777' (length=4)
6000 =>
array (size=2)
0 =>
array (size=2)
0 => string '437' (length=3)
1 => string '6000' (length=4)
1 =>
array (size=2)
0 => string '437' (length=3)
1 => string '6000' (length=4)
Is it what you want?
Group array values based on key in php?
You could use a generic function:
function _group_by($array, $key) {
$return = array();
foreach($array as $val) {
$return[$val[$key]][] = $val;
}
return $return;
}
I added some sample code to test
<?php
$list= [
[ 'No' => 101,
'Paper_id' => 'WE3P-1',
'Title' => "a1",
'Author' => 'ABC',
'Aff_list' => "University of South Florida, Tampa, United States",
'Abstracts' => "SLA"
] ,
[ 'No' => 101,
'Paper_id' => 'WE3P-1',
'Title' => "a2",
'Author' => 'DEF',
'Aff_list' => "University of South Florida, Tampa, United States",
'Abstracts' => "SLA"
] ,
[ 'No' => 104,
'Paper_id' => 'TUSA-3',
'Title' => "a3",
'Author' => 'GH1',
'Aff_list' => "University of Alcala, Alcala de Henares, Spain",
'Abstracts' => "Microwave"
] ];
print_r(_group_by($list, 'No'));
Group and merge subarray data based on one column value
A simple loop should do this..
$group = [];
foreach ($data as $item) {
if (!isset($group[$item['date']])) {
$group[$item['date']] = [];
}
foreach ($item as $key => $value) {
if ($key == 'date') continue;
$group[$item['date']][$key] = $value;
}
}
Group PHP array elements based on single value in each array
What I believe you want to do is:
$grouped_types = array();
foreach($invalid_results as $type){
$grouped_types[$type['another_id']][] = $type['name'];
}
var_dump($grouped_types);
Output:
array (size=3)
5 =>
array (size=1)
0 => string 'Test 1' (length=6)
3 =>
array (size=3)
0 => string 'Test 2' (length=6)
1 => string 'Test 7' (length=6)
2 => string 'Test 10' (length=7)
2 =>
array (size=1)
0 => string 'Test 3' (length=6)
Grouping arrays in PHP
Just iterate over the array and use another array for the groups. It should be fast enough and is probably faster than the overhead involved when using sqlite or similar.
$groups = array();
foreach ($data as $item) {
$key = $item['key_to_group'];
if (!isset($groups[$key])) {
$groups[$key] = array(
'items' => array($item),
'count' => 1,
);
} else {
$groups[$key]['items'][] = $item;
$groups[$key]['count'] += 1;
}
}
Related Topics
How to Get Info on Sent PHP Curl Request
How Validate Unique Email Out of the User That Is Updating It in Laravel
Rationale Behind Simplexmlelement's Handling of Text Values in Addchild and Addattribute
Chart.Js - Getting Data from Database Using MySQL and PHP
Parsing JavaScript (Not JSON) in PHP
How to Send Email with Smtp in PHP
Can Closing the Browser Terminate the PHP Script on the Server
Woocommerce - How to Create Multiple Single Product Template Based on Category
Why Does PHP Allow "Incompatible" Constructors
How to Send Multiple Attachment in Single Mail in PHP
Laravel Pluck Fields from Relations
Detecting Whether a User Is Behind a Proxy
Best Way to Periodically Execute a PHP Script
Php-Sort Array Based on Another Array