Adding Values them removing duplicates in a php array - php

I have an array that I need to process by adding the values, then removing any added duplicates.
Here's the Original Array...
Array (
[0] => Array ( [PID] => 2872 [QTY] => 1 )
[1] => Array ( [PID] => 3145 [QTY] => 2 )
[2] => Array ( [PID] => 3107 [QTY] => 1 )
[3] => Array ( [PID] => 2739 [QTY] => 1 )
[4] => Array ( [PID] => 3137 [QTY] => 1 )
[5] => Array ( [PID] => 3107 [QTY] => 1 )
[6] => Array ( [PID] => 2739 [QTY] => 1 )
[7] => Array ( [PID] => 3107 [QTY] => 1 )
[8] => Array ( [PID] => 3137 [QTY] => 4 )
[9] => Array ( [PID] => 3551 [QTY] => 1 )
[10] => Array ( [PID] => 3107 [QTY] => 1 )
[11] => Array ( [PID] => 3107 [QTY] => 1 )
[12] => Array ( [PID] => 3137 [QTY] => 1 )
[13] => Array ( [PID] => 3551 [QTY] => 2 )
[14] => Array ( [PID] => 3136 [QTY] => 1 )
[15] => Array ( [PID] => 3137 [QTY] => 1 )
[16] => Array ( [PID] => 3032 [QTY] => 1 )
[17] => Array ( [PID] => 3551 [QTY] => 1 )
[18] => Array ( [PID] => 3107 [QTY] => 1 )
[19] => Array ( [PID] => 3459 [QTY] => 1 )
[20] => Array ( [PID] => 3141 [QTY] => 1 )
[21] => Array ( [PID] => 2724 [QTY] => 1 )
[22] => Array ( [PID] => 2743 [QTY] => 1 )
[23] => Array ( [PID] => 3139 [QTY] => 2 )
[24] => Array ( [PID] => 3137 [QTY] => 2 )
[25] => Array ( [PID] => 3107 [QTY] => 1 )
)
What I need to do is take this array and Add the [QTY] values from the same [PID] values then after this end up with an array like this...
Array (
[0] => Array ( [PID] => 2724 [QTY] => 1 )
[1] => Array ( [PID] => 2739 [QTY] => 2 )
[2] => Array ( [PID] => 2743 [QTY] => 1 )
[3] => Array ( [PID] => 2872 [QTY] => 1 )
[4] => Array ( [PID] => 3032 [QTY] => 1 )
[5] => Array ( [PID] => 3107 [QTY] => 7 )
[6] => Array ( [PID] => 3136 [QTY] => 1 )
[7] => Array ( [PID] => 3137 [QTY] => 9 )
[8] => Array ( [PID] => 3139 [QTY] => 2 )
[9] => Array ( [PID] => 3141 [QTY] => 1 )
[10] => Array ( [PID] => 3145 [QTY] => 2 )
[11] => Array ( [PID] => 3459 [QTY] => 1 )
[12] => Array ( [PID] => 3551 [QTY] => 4 )
)
So add all the QTY then remove the duplicates.
I'm not quite sure the best way to proceed here. Any suggestions?

Loop over them, make a new array with PID as key and QTY as value so you can add the latter up.
foreach ($array as $row) {
list($pid, $qty) = $row;
$sums[$pid] += $qty;
}
Obviously you could throw an isset in to suppress the notices.
And if you want your former array structure, convert it once more.

Id just use a simple loop:
$combined = array();
// $org will be the original structure
foreach($org as $id => $data) {
$pid = $data['PID'];
if(!isset($combined[$pid])) {
$combined[$pid] = $data;
} else {
$combined[$pid]['QTY'] += $data['QTY'];
}
}
// return keys to normal indexs instead of the PID
array_values($combined);

Related

Aggregate values in multidimensional array PHP

From start I have a list of orders in the following way:
(
[product_id] => 1094
[total_weight] => 0.0390
[qty_ordered] => 10.0000
[category_id] => 12
)
What I want is to sum the values for total_weight and qty_ordered based on category_id
I have managed to get the array in the below manner now using a foreach. But I need to aggregate the values somehow.
Array
(
[14] => Array
(
[0] => Array
(
[product_id] => 1099
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
[1] => Array
(
[product_id] => 1095
[total_weight] => 0.0395
[qty_ordered] => 10.0000
)
)
[12] => Array
(
[0] => Array
(
[product_id] => 1094
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
)
[15] => Array
(
[0] => Array
(
[product_id] => 742
[total_weight] => 0.1250
[qty_ordered] => 1.0000
)
)
)
So ideally I would the output to be like with below with as little code as possible:
Array
(
[14] => Array
(
[total_weight] => 0.0785
[qty_ordered] => 20.0000
)
[12] => Array
(
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
[15] => Array
(
[total_weight] => 0.1250
[qty_ordered] => 1.0000
)
)
I was able to sort it in the below way.
foreach($retval as $val) {
$return[$val['category_id']]['qty_ordered'] += $val['qty_ordered'];
$return[$val['category_id']]['row_total'] += $val['row_total'];
}

I need to pull data out of this array into variables

I have an array, which I have created by importing a csv file with the parsecsv library.
Array (
[0] => Array ( [cubcode] => EC023 [qty] => 2 )
[1] => Array ( [cubcode] => EC026A [qty] => 3 )
[2] => Array ( [cubcode] => EC025 [qty] => 7 )
[3] => Array ( [cubcode] => EC027 [qty] => 67 )
[4] => Array ( [cubcode] => EC031 [qty] => 567 )
[5] => Array ( [cubcode] => EC033 [qty] => 78 )
[6] => Array ( [cubcode] => EC034 [qty] => 234 )
[7] => Array ( [cubcode] => EC038 [qty] => 67 )
[8] => Array ( [cubcode] => EC038A [qty] => 67 )
[9] => Array ( [cubcode] => EC039 [qty] => 60 )
[10] => Array ( [cubcode] => EC039A [qty] => 100 )
)
I need to loop through the array to create two variables "cubcode" and "qty" and use them in a function that I have already created.
I am struggling to get to them because the seem to be nested in the array. I have not done much work with arrays, so please be gentle with me.
Thanks.
Sorry for the code only answer, but you literally just need to write it in PHP, and not in English. Since $csv->data (from comments) is the name of the array:
foreach ($csv->data as $item) {
function_you_already_created($item['cubcode'], $item['qty']);
}

Convert php array to select box options

I have an input category array with childs as follows
I want to convert it using a recursive function to another array like
OUTPUT NEEDED
['1'=>'fashion', '10' => 'fashion > women','23'=> 'fashion > women > clothing','29'=> 'fashion > women > clothing > dresses' ... and so on ]
My purpose is to use output array to populate select box options to adda category.
INPUT ARRAY
Array
(
[1] => Array
(
[id] => 1
[name] => fashion
[parent_id] => 0
[childs] => Array
(
[10] => Array
(
[id] => 10
[name] => women
[parent_id] => 1
[childs] => Array
(
[23] => Array
(
[id] => 23
[name] => clothing
[parent_id] => 10
[childs] => Array
(
[29] => Array
(
[id] => 29
[name] => dresses
[parent_id] => 23
[childs] => Array
(
)
)
[30] => Array
(
[id] => 30
[name] => jumpsuits
[parent_id] => 23
[childs] => Array
(
)
)
)
)
[24] => Array
(
[id] => 24
[name] => bags & accessories
[parent_id] => 10
[childs] => Array
(
)
)
[25] => Array
(
[id] => 25
[name] => shoes
[parent_id] => 10
[childs] => Array
(
)
)
[26] => Array
(
[id] => 26
[name] => watches
[parent_id] => 10
[childs] => Array
(
)
)
[27] => Array
(
[id] => 27
[name] => jewelery
[parent_id] => 10
[childs] => Array
(
)
)
[28] => Array
(
[id] => 28
[name] => eye-wear
[parent_id] => 10
[childs] => Array
(
)
)
)
)
[11] => Array
(
[id] => 11
[name] => men
[parent_id] => 1
[childs] => Array
(
)
)
[12] => Array
(
[id] => 12
[name] => kids
[parent_id] => 1
[childs] => Array
(
)
)
[13] => Array
(
[id] => 13
[name] => sports
[parent_id] => 1
[childs] => Array
(
)
)
[14] => Array
(
[id] => 14
[name] => bags
[parent_id] => 1
[childs] => Array
(
)
)
[15] => Array
(
[id] => 15
[name] => eyewear
[parent_id] => 1
[childs] => Array
(
)
)
[16] => Array
(
[id] => 16
[name] => watches & jewelery
[parent_id] => 1
[childs] => Array
(
)
)
)
)
[2] => Array
(
[id] => 2
[name] => supermarket
[parent_id] => 0
[childs] => Array
(
[17] => Array
(
[id] => 17
[name] => food & beverages
[parent_id] => 2
[childs] => Array
(
[31] => Array
(
[id] => 31
[name] => breakfast
[parent_id] => 17
[childs] => Array
(
)
)
[32] => Array
(
[id] => 32
[name] => snacks
[parent_id] => 17
[childs] => Array
(
)
)
)
)
[18] => Array
(
[id] => 18
[name] => dairy products
[parent_id] => 2
[childs] => Array
(
)
)
[19] => Array
(
[id] => 19
[name] => beauty
[parent_id] => 2
[childs] => Array
(
)
)
[20] => Array
(
[id] => 20
[name] => homecare
[parent_id] => 2
[childs] => Array
(
)
)
[21] => Array
(
[id] => 21
[name] => baby world
[parent_id] => 2
[childs] => Array
(
)
)
[22] => Array
(
[id] => 22
[name] => pet world
[parent_id] => 2
[childs] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[name] => electronics
[parent_id] => 0
[childs] => Array
(
[33] => Array
(
[id] => 33
[name] => laptops
[parent_id] => 3
[childs] => Array
(
)
)
[34] => Array
(
[id] => 34
[name] => television
[parent_id] => 3
[childs] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[name] => mobiles & tablets
[parent_id] => 0
[childs] => Array
(
[35] => Array
(
[id] => 35
[name] => mobiles
[parent_id] => 4
[childs] => Array
(
)
)
[36] => Array
(
[id] => 36
[name] => tablets
[parent_id] => 4
[childs] => Array
(
)
)
)
)
[5] => Array
(
[id] => 5
[name] => baby & toys
[parent_id] => 0
[childs] => Array
(
)
)
[6] => Array
(
[id] => 6
[name] => home
[parent_id] => 0
[childs] => Array
(
)
)
[7] => Array
(
[id] => 7
[name] => perfumes & beauty
[parent_id] => 0
[childs] => Array
(
)
)
[8] => Array
(
[id] => 8
[name] => sports & fitness
[parent_id] => 0
[childs] => Array
(
)
)
[9] => Array
(
[id] => 9
[name] => automotive
[parent_id] => 0
[childs] => Array
(
)
)
)
I need an output array with key,the category id
You can create array by this way:
$array = array(
'category_1' => 'value',
'category_2' => array(
'subcategory_1' => 'value',
'subcategory_2' => 'value',
),
);

Looping a Complex Multidimensional Array

I'm trying to extract all the variables from a complex array of nutrition results from Neutronix API.
The array is as follows:
Food Array:
Array ( [foods] =>
Array ( [0] => Array (
[food_name] => kale
[brand_name] =>
[serving_qty] => 1
[serving_unit] => cup, chopped
[serving_weight_grams] => 130
[nf_calories] => 36.4
[nf_total_fat] => 0.52
[nf_saturated_fat] => 0.07
[nf_cholesterol] => 0
[nf_sodium] => 29.9
[nf_total_carbohydrate] => 7.32
[nf_dietary_fiber] => 2.6
[nf_sugars] => 1.63
[nf_protein] => 2.47
[nf_potassium] => 296.4
[nf_p] => 36.4
[full_nutrients] => Array (
[0] => Array (
[attr_id] => 203
[value] => 2.47
)
[1] => Array (
[attr_id] => 204
[value] => 0.52
)
[2] => Array (
[attr_id] => 205
[value] => 7.319
)
[3] => Array (
[attr_id] => 207
[value] => 1.131
)
[4] => Array (
[attr_id] => 208
[value] => 36.4
)
[5] => Array (
[attr_id] => 221
[value] => 0
)
[6] => Array (
[attr_id] => 255
[value] => 118.56
)
[7] => Array (
[attr_id] => 262
[value] => 0
)
[8] => Array (
[attr_id] => 263
[value] => 0
)
[9] => Array (
[attr_id] => 268
[value] => 152.1
)
[10] => Array (
[attr_id] => 269
[value] => 1.625
)
[11] => Array (
[attr_id] => 291
[value] => 2.6
)
[12] => Array (
[attr_id] => 301
[value] => 93.6
)
[13] => Array (
[attr_id] => 303
[value] => 1.17
)
[14] => Array (
[attr_id] => 304
[value] => 23.4
)
[15] => Array (
[attr_id] => 305
[value] => 36.4
)
[16] => Array (
[attr_id] => 306
[value] => 296.4
)
[17] => Array (
[attr_id] => 307
[value] => 29.9
)
[18] => Array (
[attr_id] => 309
[value] => 0.312
)
[19] => Array (
[attr_id] => 312
[value] => 0.2028
)
[20] => Array (
[attr_id] => 315
[value] => 0.5408
)
[21] => Array (
[attr_id] => 317
[value] => 1.17
)
)
[nix_brand_name] =>
[nix_brand_id] =>
[nix_item_name] =>
[nix_item_id] =>
[upc] =>
[consumed_at] => 2017-09-08T22:44:54+00:00
[metadata] => Array ( )
[source] => 1
[ndb_no] => 11234
[tags] => Array (
[item] => kale
[measure] =>
[quantity] => 1.0
[tag_id] => 644
)
[alt_measures] => Array (
[0] => Array (
[serving_weight] => 130
[measure] => cup, chopped
[seq] => 1
[qty] => 1 )
[1] => Array (
[serving_weight] => 130
[measure] => cup
[seq] => 80
[qty] => 1 )
[2] => Array (
[serving_weight] => 2.71
[measure] => tsp
[seq] => 101
[qty] => 1 )
[3] => Array (
[serving_weight] => 8.13
[measure] => tbsp
[seq] => 102
[qty] => 1 )
)
[lat] =>
[lng] =>
[meal_type] => 5
[photo] => Array (
[thumb] => https://d2xdmhkmkbyw75.cloudfront.net/644_thumb.jpg
[highres] => https://d2xdmhkmkbyw75.cloudfront.net/644_highres.jpg
)
[sub_recipe] =>
)
)
)
My PHP Code so far is below. I've inserted comments where I am stuck as to how to get the variables.
I'm also not sure if I'm looping the segments of the array correctly.
foreach ($foods as $food){
foreach($food as $key => $val) {
//get values for each variable
foreach($full_nutrients as $nutrient){
foreach($nutrient as $key => $val){
//get values for each variable
}
}
foreach($metadata as $meta){
$source = $meta['source'];
$ndb_no = $meta['ndb_no'];
foreach($tags as $tag){
$tag_item = $tag['item'];
$tag_measure = $tag['measure'];
$tag_quantity = $tag['quantity'];
$tag_id = $tag['tag_id'];
}
}
foreach($alt_measures as $alt_meaasure){
foreach($alt_meaasure as $key => $val){
//get alt_measures
}
}
foreach($photo as $image){
$image_thumb = $image['thumb'];
$image_highres = $image['highres'];
}
}
}

Multidimensional 2d array sort

here is my array:
Array
(
[0] => Array
(
[product_option_id] => 1072
[option_id] => 5
[name] => Sizes
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 8282
[option_value_id] => 57
[name] => 10
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 8283
[option_value_id] => 58
[name] => 11
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 8284
[option_value_id] => 59
[name] => 12
[price] =>
[price_prefix] => +
)
[3] => Array
(
[product_option_value_id] => 8285
[option_value_id] => 60
[name] => 13
[price] =>
[price_prefix] => +
)
[4] => Array
(
[product_option_value_id] => 8279
[option_value_id] => 61
[name] => 7
[price] =>
[price_prefix] => +
)
[5] => Array
(
[product_option_value_id] => 8280
[option_value_id] => 62
[name] => 8
[price] =>
[price_prefix] => +
)
[6] => Array
(
[product_option_value_id] => 8281
[option_value_id] => 63
[name] => 9
[price] =>
[price_prefix] => +
)
)
[required] => 1
)
)
is it possible to sort by [option_value][$i][name] ? so output of this should be:
Array
(
[0] => Array
(
[product_option_id] => 1072
[option_id] => 5
[name] => Sizes
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 8279
[option_value_id] => 61
[name] => 7
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 8280
[option_value_id] => 62
[name] => 8
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 8281
[option_value_id] => 63
[name] => 9
[price] =>
[price_prefix] => +
)
[3] => Array
(
[product_option_value_id] => 8282
[option_value_id] => 57
[name] => 10
[price] =>
[price_prefix] => +
)
[4] => Array
(
[product_option_value_id] => 8283
[option_value_id] => 58
[name] => 11
[price] =>
[price_prefix] => +
)
[5] => Array
(
[product_option_value_id] => 8284
[option_value_id] => 59
[name] => 12
[price] =>
[price_prefix] => +
)
[6] => Array
(
[product_option_value_id] => 8285
[option_value_id] => 60
[name] => 13
[price] =>
[price_prefix] => +
)
)
[required] => 1
)
)
I lost many hours on this, if somebody could, please help me with it (tryed array_multisort but no result)
PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column()
array_multisort(array_column($array[0]['option_value'], 'name'),
SORT_ASC, $array[0]['option_value']);

Categories