PHP combine array based on value of particular element - php

I have an array that looks like this
Array (
[0] => Array
(
[id] => 123
[count] => 1
)
[1] => Array
(
[id] => 123
[count] => 3
)
[2] => Array
(
[id] => 513
[count] => 0
)
[3] => Array
(
[id] => 561
[count] => 1
)
[4] => Array
(
[id] => 613
[count] => 7
)
)
What I want to do is create a new array, that totals the count where the id values are the same. So for example, the new array would look like this:
Array (
[0] => Array
(
[id] => 123
[count] => 4
)
[1] => Array
(
[id] => 513
[count] => 0
)
[2] => Array
(
[id] => 561
[count] => 1
)
[3] => Array
(
[id] => 613
[count] => 7
)
)
Would anyone know a good method to do this?
Thank you!

Short and simple:
$new_array = array();
foreach($data_array as $value) {
if(array_key_exists($value['id'], $new_array)) {
$new_array[$value['id']] += $value['count'];
} else {
$new_array[$value['id']] = $value['count'];
}
}
echo print_r($new_array,true);
I made it id => value since there didn't seem to need another array for data when the id could be used as the array's key.

Any reason why you are not making an associative array of id => count

You can do it with the following function (I didn't tested the code, but it should work ):
function get_count_array($arr) {
$new_array = array();
foreach($arr as $item) {
$found = false;
//loop through all the current new items to check if we already have it
for($i = 0; $i < count($new_array); $i++) {
//do we have it?
if($new_array[$i]['id'] == $item['id']) {
$new_array[$i]['count'] += 1;
$found = true;
break;
}
}
if(!$found) {
$new_array[] = array('id' => $item['id'], 'count' => 0);
}
}
return $new_array;
}

Related

How to stop a recursive loop when a certain condition is met in PHP

I have an array of the below stucture and I want get the element from the root level to the 3rd level
Array
(
[0] => Array
(
[id] => 1
[parent_id] =>
[name] => try
[sub_categories] => Array
(
[0] => Array
(
[id] => 2
[parent_id] => 1
[name] => try1
[sub_categories] => Array
(
[0] => Array
(
[id] => 3
[parent_id] => 2
[name] => try2
[sub_categories] => Array
(
[0] => Array
(
[id] => 4
[parent_id] => 3
[name] => try3
[sub_categories] => Array
(
[0] => Array
(
[id] => 5
[parent_id] => 4
[name] => try4
)
)
)
)
)
)
)
)
(
[1] => Array
(
[id] => 6
[parent_id] => 1
[name] => try1
[sub_categories] => Array
(
[0] => Array
(
[id] => 7
[parent_id] => 6
[name] => try2
[sub_categories] => Array
(
[0] => Array
(
[id] => 8
[parent_id] => 7
[name] => try3
[sub_categories] => Array
(
[0] => Array
(
[id] => 9
[parent_id] => 8
[name] => try4
)
)
)
)
)
)
)
)
)
)
public function getElements( $parent_id = NULL, $level = 0 ) {
$data = models\Categories::find()->where( ['parent_id'=>$parent_id] )->all();
$arr = array();
foreach( $data as $data ) {
if( $level == 3 ) {
break;
}
//do something
$countChilds = models\Categories::find()->where( ['parent_id'=>$data->id] )->count();
if( $countChilds > 0 ){
$catData['sub_categories'] = $this->getElements( $parent_id = $data->id, $level++ );
}
$arr[] = $catData;
}
return $arr;
}
I tried to add a counter and break out of the loop when the counter gets to 3 but it's not working, so i think since the counter can only work on the immediate sub-categories that is next to the root category and since i want to go down the tree 3 level so the counter isn't working what is the best way to achieve this I'd like my resulting array to out put an array with ID 1,2,3,4 , 6,7,8. element with ID 5,9 should not be printed since they belong to level 4
Any help on this would be appreciated
<?php
public function getElements( $parent_id = NULL, $level = 0 ) {
if( $level == 3 ) return []; // or return $arr;
$data = models\Categories::find()->where( ['parent_id'=>$parent_id] )->all();
$arr = array();
foreach( $data as $data ) {
//do something
$countChilds = models\Categories::find()->where( ['parent_id'=>$data->id] )->count();
if( $countChilds > 0 ){
$catData['sub_categories'] = $this->getElements( $parent_id = $data->id, $level + 1);
}
$arr[] = $catData;
}
return $arr;
}
In the above code, I made 2 changes.
If $level == 3, then return it instantly. No need to put that in the foreach.
It should be $catData['sub_categories'] = $this->getElements( $parent_id = $data->id, $level + 1); instead of $level++, since the child will have parent level + 1 and post increment operator doesn't do that. Also, even ++$level(pre-increment operation) would be a bad idea since you are modifying the variable even for the next upcoming entries in the foreach loop. So, $level + 1 is the correct way.

count same categories from array and Store in new array with its count and category name

I have one array which I am getting from database query response.
Now I want to count same categories and print in option_name array.
I have tried it with different array functions but want get desire output.
I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.
Array
(
[0] => Array
(
[CNC] => Array
(
[id] => 5
[category_id] => 68
)
[GVO] => Array
(
[option_name] => Actors
)
)
[1] => Array
(
[CNC] => Array
(
[id] => 11
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[2] => Array
(
[CNC] => Array
(
[id] => 3
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[3] => Array
(
[CNC] => Array
(
[id] => 4
[category_id] => 74
)
[GVO] => Array
(
[option_name] => Musician
)
)
[4] => Array
(
[CNC] => Array
(
[id] => 7
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
[5] => Array
(
[CNC] => Array
(
[id] => 6
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
)
Desire Output:
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
Thanks in advance!
You simply need to loop through the array using foreach like as
$result = [];
foreach($arr as $k => $v){
if(isset($result[$v['GVO']['option_name']])){
$result[$v['GVO']['option_name']] += 1;
}else{
$result[$v['GVO']['option_name']] = 1;
}
}
print_R($result);
You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:
$counts = [];
foreach($array as $v) {
if(!isset($counts[$v['GVO']['option_name']])) {
$counts[$v['GVO']['option_name']] = 0;
}
$counts[$v['GVO']['option_name']]++;
}
print_r($counts);
Try this:
$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
if(in_array($value, $new_array)){
$new_array[$value] = $new_array[$value]+1;
}else{
$new_array[$value] = 1;
}
}
Output
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)

list duplicates in PHP Array

i have a new problem and my mind is burning, i have an array in php
$test = array();
Array
(
[0] => Array
(
[account] => 14319896
[value] => 725.57
[id] => 280
)
[1] => Array
(
[account] => 163157
[value] => -723.57
[id] => 283
)
[2] => Array
(
[account] => 163157
[value] => 723.57
[id] => 284
)
[3] => Array
(
[account] => 161817
[value] => -723.57
[id] => 285
)
)
i need the accounts, they are more than one in this array, in this example i need $test[1][id] and $test[2][id]
have you an idea? i have no idea more at this time.
Thanks for your help.
Use the account number as key in a new array, count each entry and then take the items with a count > 1
$dupes = array();
foreach($array as $account) {
++$dupes[$account['account']];
}
$dupes = array_filter($dupes, function($count) { return $count > 1; });
Editing to answer the comments below the question …
If you want the IDs (or keys) of the duplicates, do not store the count directly, but use another array instead.
$dupes = array();
foreach($array as $key => $account) {
if(!array_key_exists($account, $dupes))
$dupes[$account['account']] = array();
$dupes[$account['account']][] = $account['id']; // or: = $key
}
$dupes = array_filter($dupes, function($ids) { return count($ids) > 1; });
You should change your architecture to have a associative array where keys are account number organized like that :
Array
(
[14319896] => Array
(
[0]=>Array(
[value] => 725.57
[id] => 280
)
)
[163157] => Array
(
[0]=>Array(
[value] => -723.57
[id] => 283
)
[1]=>Array(
[value] => 723.57
[id] => 284
)
)
[161817] => Array
(
[0]=>Array(
[value] => -723.57
[id] => 285
)
)
)
$dupes = array();
foreach($array as $key => $account) {
$dupes[$account['account']][] = array($account['id'],$account['value']);
}
Whith that architecture you can get everything you want thanks to array_filter :
$dups = array_filter($dupes, function($account){ return count($account)>1;});

PHP compare two arrays but foreach around them (or alt method)

So i have this array:
Array
(
[0] => Array
(
[Page] => Array
(
[id] => 2
)
[PageRevision] => Array
(
[PageId] => 1
[PageRevisionId] => 3
[goldMaster] => 0
)
[PageLanguage] => Array
(
[name] => Contact Page
)
[PageSetting] => Array
(
[url] => contact2
)
)
[1] => Array
(
[Page] => Array
(
[id] => 2
)
[PageRevision] => Array
(
[PageId] => 1
[PageRevisionId] => 2
[goldMaster] => 1
)
[PageLanguage] => Array
(
[name] => Contact Page 2
)
[PageSetting] => Array
(
[url] => contact
)
)
)
What i need to do is determine , out of the two array given, if one of them has a higher PageRevisionId than the other one and if it's goldMaster is set to 0. But i am struggling to find any method to do this.
if ($array[0]['PageRevision']['PageRevisionId'] > $array[1]['PageRevision']['PageRevisionId']
&& $array[0]['PageRevision']['goldMaster'] == 0) {
// your code
}
You can use this code with an unlimited number of element in you main array:
$max_pageRevisionId = 0;
$max_goldMaster = 0;
foreach($myarray as $key => $value) {
if($value['PageRevision']['PageRevisionId'] > $max_pageRevisionId) {
$max_pageRevisionId = $value['PageRevision']['PageRevisionId'];
$max_goldMaster = $value['PageRevision']['goldMaster'];
}
}
if($max_goldMaster > 0) {
// Do something
} else {
// Do something else
}
$elem = ($array [0]['PageRevision']['PageRevisionId'] > $array [1]['PageRevision']['PageRevisionId'])?
1:
0;
var_dump ($array [0]['PageRevision']['goldMaster '] == 0)

Looping through array and totalling values

I require a bit of assistance, if someone would be kind enough to help.
I have an array with values, which I want to loop through, and if any of the 'user_id values' are the same then to total the 'max_score' value of the duplicate user id's.
Array
(
[0] => Array
(
[user_id] => 2
[max_score] => 10081
)
[1] => Array
(
[user_id] => 1
[max_score] => 8774
)
[2] => Array
(
[user_id] => 2
[max_score] => 5477
)
[3] => Array
(
[user_id] => 3
[max_score] => 5267
)
[4] => Array
(
[user_id] => 1
[max_score] => 5010
)
)
Would anyone know how to accomplish this?
Many thanks.
$totals = array();
foreach ($your_array_values as $v) {
$id = $v['user_id'];
if (isset($totals[$id])) {
$totals[$id] += $v['max_score'];
} else {
$totals[$id] = $v['max_score'];
}
}
you need a second array with the user ids as key. You can do it like this:
$scoresums = array();
foreach ($yourarray AS $user_score) {
if (!isset($scoresums[$user_score['user_id']])) $scoresums[$user_score['user_id']] = 0;
$scoresums[$user_score['user_id']] += $user_score['max_score'];
}
The third line will prevent php from throwing notices.

Categories