Sum values in multidimensional array? - php

I just have no idea how to sum and echo values in multidimensional array. This is my array with info about order.
I need to sum RESULT_VALUE and echo one united DESCR list. I have tried lots of things but nothing, never worked with that kind of arrays levels. This is an array with two different products with applied discounts.
Array
(
[0] => Array
(
[ORDER_ID] => 35
[DISCOUNT_ID] => 18
[ORDER_COUPON_ID] => 0
[COUPON_ID] =>
[RESULT] => Array
(
[BASKET] => Array
(
[617] => Array
(
[RULE_ID] => 248
[APPLY] => Y
[RULE_DESCR_ID] => 248
[ACTION_BLOCK_LIST] => Array
(
[0] => 0
)
[DESCR_DATA] => Array
(
[0] => Array
(
[TYPE] => 2
[VALUE] => 10
[VALUE_TYPE] => P
[VALUE_ACTION] => D
[RESULT_VALUE] => 229.905
[RESULT_UNIT] => RUB
)
)
[DESCR] => Array
(
[0] => discount 10% (229.91 rub.)
)
[DESCR_ID] => 248
[BASKET_ID] => 617
[MODULE] => catalog
[PRODUCT_ID] => 10109
)
[618] => Array
(
[RULE_ID] => 249
[APPLY] => Y
[RULE_DESCR_ID] => 249
[ACTION_BLOCK_LIST] => Array
(
[0] => 0
)
[DESCR_DATA] => Array
(
[0] => Array
(
[TYPE] => 2
[VALUE] => 10
[VALUE_TYPE] => P
[VALUE_ACTION] => D
[RESULT_VALUE] => 70.796
[RESULT_UNIT] => RUB
)
)
[DESCR] => Array
(
[0] => discount 10% (70.80 rub.)
)
[DESCR_ID] => 249
[BASKET_ID] => 618
[MODULE] => catalog
[PRODUCT_ID] => 10054
)
)
)
[ACTION_BLOCK_LIST] => 1
)

You can use array_walk_recursive to get result. Code below sum all values of key $key wherever they are in the array
$key = 'RESULT_VALUE';
$sum = 0;
array_walk_recursive ($array,
function($v, $k, $key) use (&$sum) {
if($k === $key) { $sum += $v;}},
$key);
echo $sum;

Echo array_sum(array_column($array, 'RESULT_VALUE'));
This solution work ?

You can use array_sum and array_column.
Array_column grabs all values from one column in the array, and array_sum... Sums it...
echo array_sum(array_column($arr, "RESULT_VALUE"));

Related

Collecting values from a multidimensional array using foreach loops

There is an array which is built looks like the following (with some more values which i left away for this example):
Array
(
[0] => Array
(
[id] => 44
[cars] => Array
(
[0] => Array
(
[id] => 38
)
[1] Array
(
[id] => 39
)
)
)
[1] => Array
(
[id] => 45
[cars] => Array
(
[0] => Array
(
[id] =>136
)
[1] =>Array
(
[id] =>137
)
[2] =>Array
(
[id] =>138
)
)
)
)
I want to build another array from the above in the following form:
Array
(
[0] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 38
)
[1] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 39
)
[2] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 136
)
[3] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 137
)
[4] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 138
)
)
I tried to achieve this with following function:
foreach($filterSortSaveArray as $filterSortSaveArray['cars'] => $value){
$id = $filterSortSaveArray['id'];
foreach($value['cars'] as $value => $car){
$field_values['car_filter_sort_id'] = $id;
$field_values['car_id'] = $car['id'];
}
}
But the the result differs from what I have expected. Any suggestions?
There are two big issues in your code. First, are you referencing undefined value with $filterSortSaveArray['cars'], since there is no 'cars' key in the first level of the original array. Second, by assigning values to $field_values['car_filter_sort_id'] and $field_values['car_id'] in the loop you are just overriding them in each iteration. You need to push the values into an array using []= operator (which is equivalent to applying array_push()).
Try this:
$result = [];
foreach($filterSortSaveArray as $k => $v) {
if (!is_array($v['cars']))
continue;
$id = $v['id'];
foreach ($v['cars'] as $i => $car){
$result[] = [
'car_filter_sort_id' => $id,
'car_id' => $car['id']
];
}
}

Compare items within an array without loosing information [duplicate]

This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 6 years ago.
I have an array of product information. I have a loop that runs through the array and compares the prices to find the lowest one.
$rows = get_field('variations',$product_id);
if($rows){
$prices = array();
foreach($rows as $row){
$prices[] = $row['variation_price'];
}
//Getting the lowest price from the array
$lowest_price = min($prices);
}
echo $lowest_price;
However I now need to get the other information that is associated with the "lowest price" array set. IE. I need the ID of the lowest price product etc.
Any help is welcome!
Here is a dump of the array
Array
(
[0] => Array
(
[] =>
[variation_title] => Small Business
[checkout] => cart
[trial] => Array
(
[0] => trial
)
[variation_price] => 7
[variation_subscription_cycle] => /month
[variation_id] => 405
[variation_url] =>
[custom] => Array
(
[0] => Array
(
[custom_field] => 1 Domain
)
[1] => Array
(
[custom_field] => 1 Million QPM
)
[2] => Array
(
[custom_field] => 50 Records
)
[3] => Array
(
[custom_field] => DNS Alias
)
)
[css_styles] =>
)
[1] => Array
(
[] =>
[variation_title] => Medium Business
[checkout] => cart
[trial] => Array
(
[0] => trial
)
[variation_price] => 35
[variation_subscription_cycle] => /month
[variation_id] => 286
[variation_url] =>
[custom] => Array
(
[0] => Array
(
[custom_field] => 10 Domains
)
[1] => Array
(
[custom_field] => 5 Million QPM
)
[2] => Array
(
[custom_field] => 500 Records
)
[3] => Array
(
[custom_field] => DNS Alias
)
)
[css_styles] => ribbon
)
[2] => Array
(
[] =>
[variation_title] => Large Business
[checkout] => cart
[trial] => Array
(
[0] => trial
)
[variation_price] => 100
[variation_subscription_cycle] => /month
[variation_id] => 406
[variation_url] =>
[custom] => Array
(
[0] => Array
(
[custom_field] => 50 Domains
)
[1] => Array
(
[custom_field] => 15 Million QPM
)
[2] => Array
(
[custom_field] => 1,,500 Records
)
[3] => Array
(
[custom_field] => DNS Alias
)
)
[css_styles] =>
)
)
<?php
class Test {
public $a;
public $b;
public function __construct($a1,$b1)
{
$this->a = $a1;
$this->b = $b1;
}
}
$first = new test(1,12);
$second = new test(4,25);
$third = new test(7,9);
$values = [$first, $second, $third];
function compare (Test $firstObject, Test $secondObject) {
return $firstObject->a > $secondObject->a;
}
usort($values, "compare");
var_dump($values);
?>

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
)

Taking Value from one array with keys aret the valuses of another another array in php

I have one array called $stocknumb
$stocknumb= Array ( [0] => 102 [1] => 103 [2] => 104 )
Another array called $price it contains both $stocknumb as well as price
$price=Array ( ['102'] => Array ( [0] => 2000 ) ['103'] => Array ( [0] => 3 ) ['104'] => Array ( [0] => 4000 ) )
Now i would like to get prices for only stock numbers which are in $stocknumb array. Both price and stock number are in $price array.
How to get it in PHP.
See array_column function (php >= 5.5) :
$price= array(
102 => array(0 => 2000),
103 => array(0 => 3),
104 => array(0 => 4000)
);
$array_price = array_column ($price, 0);
print_r($array_price);
/*Array
(
[0] => 2000
[1] => 3
[2] => 4000
)
*/
If you don't use php 5.5, see :
https://github.com/ramsey/array_column/blob/master/src/array_column.php
If you want final data as array then with conventional way of looping could be done as
$stocknumb= Array ( 0 => 102, 1 => 103, 2 => 104 ) ;
$price=Array ( '102' => Array ( 0 => 2000 ), '103' => Array ( 0 => 3 ), '104' => Array ( 0 => 4000 ) );
$final_array = array() ;
foreach($stocknumb as $key=>$val){
if(array_key_exists($val,$price)){
$final_array[$val] = $price[$val][0];
}
}
print_r($final_array);
Output
Array ( [102] => 2000 [103] => 3 [104] => 4000 )
You can use array intersect function
$stocknumb= array ( 102, 103 );
// Make the key same as the value.
$stocknumb = array_combine($stocknumb, $stocknumb);
$price= array (102 => array (2000), 103 => array (3), 104 => array (4000 ));
$filtered = array_intersect_key($price, $stocknumb);
A single line of code with array_flip() and array_key_intersect() will suffice
$new_arr=array_intersect_key($price, array_flip($stocknumb));
OUTPUT :
Array
(
[102] => Array
(
[0] => 2000
)
[103] => Array
(
[0] => 3
)
[104] => Array
(
[0] => 4000
)
)
<?php
foreach($price as $key => $value) {
if(in_array($key, $stocknumb)) {
echo $value[0];
}
}
DEMO

how to merge key array on array

and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.

Categories