How to delete an array list based on key? - php

For instance :
Array
(
[0] => Array
(
[id] => 23
[merchant_id] => 23
)
[1] => Array
(
[id] => 24
[merchant_id] => 46
)
)
I want to delete the list which merchant_id except 46, after the operation:
Array
(
[0] => Array
(
[id] => 24
[merchant_id] => 46
)
)
how the best way to removing this array list?

You can get result using array_filter as follows
// suppose your data is in $data variable
$data = [
['id' => 23,
'merchant_id' => 23],
['id' => 23,
'merchant_id' => 46],
];
//return true only if marchant_id == 46
$filtered_array = array_filter($data,function($datum){
return $datum["merchant_id"] == 46;
});

Hi Please check below code
$array = array(
array('id' => 23, 'merchant_id' => 23),
array('id' => 24, 'merchant_id' => 46),
array('id' => 25, 'merchant_id' => 34),
array('id' => 26, 'merchant_id' => 46),
);
$final = array();
foreach ($array as $key => $value) {
if($value['merchant_id'] == 46){
$final[] = $value;
}
}
print_r($final);

If you are struggling with the array functions a simple foreach loop and a test of the field you are interested in would do nicely
$A = [ ['id' => 23, 'merchant_id' => 23 ],
['id' => 24, 'merchant_id' => 46 ],
['id' => 25, 'merchant_id' => 21 ],
['id' => 26, 'merchant_id' => 29 ],
];
foreach ( $A as $key => $t ) {
if( $t['merchant_id'] != 46 ){
unset($A[$key]);
}
}
print_r($A);
RESULT
Array
(
[1] => Array
(
[id] => 24
[merchant_id] => 46
)
)

Related

multidimensional array php - sum values with same groupRange

I will try to explain my problem in small examples:
I have a multidimensional array that represents data from the database, lets's say the input looks like this:
Array
(
[0] => Array
(
[groupRange] => 20-25
[value] => 12
[followersFemaleRate] => 12
[followersMaleRate] => 14
)
[1] => Array
(
[groupRange] => 30-44
[value] => 32
[followersFemaleRate] => 17
[followersMaleRate] => 3
)
[2] => Array
(
[groupRange] => 30-44
[value] => 88
[followersFemaleRate] => 17
[followersMaleRate] => 3
)
)
What I want? To sum value, followersFemaleRate, followersMaleRate with the same groupRange, so the output should be this:
Array
(
[0] => Array
(
[groupRange] => 20-25
[value] => 12
[followersFemaleRate] => 12
[followersMaleRate] => 14
)
[1] => Array
(
[groupRange] => 30-44
[value] => 120
[followersFemaleRate] => 34
[followersMaleRate] => 6
)
)
My code:
$RangeArray = [];
foreach($dbProfile->getData() as $d) {
foreach ($d->getGroupPercentages() as $x){
$ageRangeSingleArray['groupRange'] = $x->getGroupRange();
$ageRangeSingleArray['value'] = $x->getValue();
$ageRangeSingleArray['followersFemaleRate'] = $x->getFollowerGenderFemale();
$ageRangeSingleArray['followersMaleRate'] = $x->getFollowerGenderMale();
$RangeArray [] = $ageRangeSingleArray;
}
}
However im stuck, my idea is to first check if groupRage already exists, if yes, sum values for that range, if not add new element groupRange with values, any help with code?
#Salines solution was good, but I offer a simple solution for the beginners...
Another simple solution to your problem:
$input = [
[
'groupRange' => '20-25',
'value' => 12,
'followersFemaleRate' => 12,
'followersMaleRate' => 14,
],
[
'groupRange' => '30-44',
'value' => 88,
'followersFemaleRate' => 17,
'followersMaleRate' => 3,
],
[
'groupRange' => '30-44',
'value' => 32,
'followersFemaleRate' => 17,
'followersMaleRate' => 3,
],
];
$groupRangeHolder = [];
$output = [];
foreach($input as $item) {
if( ! array_key_exists( $item['groupRange'] , $groupRangeHolder ) )
{
$groupRangeHolder[$item['groupRange']]['value'] = $item['value'];
$groupRangeHolder[$item['groupRange']]['followersFemaleRate'] = $item['followersFemaleRate'];
$groupRangeHolder[$item['groupRange']]['followersMaleRate'] = $item['followersMaleRate'];
}
else
{
$groupRangeHolder[$item['groupRange']]['value'] += $item['value'];
$groupRangeHolder[$item['groupRange']]['followersFemaleRate'] += $item['followersFemaleRate'];
$groupRangeHolder[$item['groupRange']]['followersMaleRate'] += $item['followersMaleRate'];
}
}
$cur = 0;
foreach($groupRangeHolder as $key => $values)
{
$output[$cur]['groupRange'] = $key;
$output[$cur]['value'] = $values['value'];
$output[$cur]['followersFemaleRate'] = $values['followersFemaleRate'];
$output[$cur++]['followersMaleRate'] = $values['followersMaleRate'];
}
echo "<pre>";
print_r($output);
echo "</pre>";
try:
$input = [
[
'groupRange' => '20-25',
'value' => 12,
'followersFemaleRate' => 12,
'followersMaleRate' => 14,
],
[
'groupRange' => '30-44',
'value' => 88,
'followersFemaleRate' => 17,
'followersMaleRate' => 3,
],
[
'groupRange' => '30-44',
'value' => 32,
'followersFemaleRate' => 17,
'followersMaleRate' => 3,
],
];
$groupedArray = [];
foreach( $input as $item ){
$groupedArray[$item['groupRange']]['groupRange'] = $item['groupRange'];
$groupedArray[$item['groupRange']]['value'] = ($groupedArray[$item['groupRange']]['value'] ?? 0) + $item['value'];
$groupedArray[$item['groupRange']]['followersFemaleRate'] = $item['followersFemaleRate'];
$groupedArray[$item['groupRange']]['followersMaleRate'] = $item['followersMaleRate'];
}
$output = array_values($groupedArray);
print_r($output);
output:
Array
(
[0] => Array
(
[groupRange] => 20-25
[value] => 12
[followersFemaleRate] => 12
[followersMaleRate] => 14
)
[1] => Array
(
[groupRange] => 30-44
[value] => 120
[followersFemaleRate] => 17
[followersMaleRate] => 3
)
)

php function to sum values from multi dimensional array

My $results array looks like this..
$results = [
'2020-09-06' => [
'Etsy' => ['total_orders' => 2, 'total_stickers' => 3, 'total_value' => 7.8300000000000001],
'Woo' => ['total_orders' => 10, 'total_stickers' => 20, 'total_value' => 100.38],
'eBay' => ['total_orders' => 17, 'total_stickers' => 18, 'total_value' => 67.359999999999999],
],
'2020-09-07' => [
'Etsy' => ['total_stickers' => 8, 'total_orders' => 4, 'total_value' => 34.920000000000002],
'Woo' => ['total_stickers' => 9, 'total_orders' => 3, 'total_value' => '52.90'],
'eBay' => ['total_stickers' => 23, 'total_orders' => 21, 'total_value' => 58.030000000000001],
]
];
I want to echo the combined sum for each individual item (total_value, total_stickers, total_orders) for each "marketplace" by date and thought i could do this if i pass the variables in a function and tried the following..
$array_value_sum = create_function('$array,$key', '$total = 0; foreach($array as $row) $total = $total + $row[$key]; return $total;');
echo "Total Current Value" . $array_value_sum($obj['results'], 'total_value');
That way I can change the variables and sum any of them with a similar echo line but this is not working for me, do I also need to specify dates in a foreach? or how can I achieve this expected output..
Array
(
[2020-09-06] => Array
(
[total_orders] => 29
[total_stickers] => 41
[total_value] => 175.5
)
[2020-09-07] => Array
(
[total_stickers] => 40
[total_orders] => 28
[total_value] => 145.85
)
)
As always, no need to over-engineer, if you know the structure, just iterate trough the values, and sum them up, this is how I would do that.
This way you can add many other marketplace and dates without later modifying the code.
<?php
$results = [
'2020-09-06' => [
'Etsy' => ['total_orders' => 2, 'total_stickers' => 3, 'total_value' => 7.8300000000000001],
'Woo' => ['total_orders' => 10, 'total_stickers' => 20, 'total_value' => 100.38],
'eBay' => ['total_orders' => 17, 'total_stickers' => 18, 'total_value' => 67.359999999999999],
],
'2020-09-07' => [
'Etsy' => ['total_stickers' => 8, 'total_orders' => 4, 'total_value' => 34.920000000000002],
'Woo' => ['total_stickers' => 9, 'total_orders' => 3, 'total_value' => '52.90'],
'eBay' => ['total_stickers' => 23, 'total_orders' => 21, 'total_value' => 58.030000000000001],
]
];
$total = ['total_stickers' => 0, 'total_orders' => 0, 'total_value' => 0];
foreach ($results as $k => $v){
foreach ($v as $k1 => $v1){
$total['total_stickers'] += $v1['total_stickers'];
$total['total_orders'] += $v1['total_orders'];
$total['total_value'] += $v1['total_value'];
}
}
var_dump($total);
/*
* array(3) {
["total_stickers"]=>
int(81)
["total_orders"]=>
int(57)
["total_value"]=>
float(321.42)
}
* */
One way would be to simply loop over it with a couple of foreach's and add the values together.
Sum all by date, including all marketplaces.
<?php
$result = [];
foreach ($data as $date => $marketplaces) {
foreach ($marketplaces as $data) {
if (!isset($result[$date])) $result[$date] = ['total_orders' => 0, 'total_stickers' => 0, 'total_value' => 0];
$result[$date] = [
'total_stickers' => $result[$date]['total_stickers'] + $data['total_stickers'],
'total_orders' => $result[$date]['total_orders'] + $data['total_orders'],
'total_value' => $result[$date]['total_value'] + $data['total_value'],
];
}
}
print_r($result);
Result:
Array
(
[2020-09-06] => Array
(
[total_stickers] => 41
[total_orders] => 29
[total_value] => 175.57
)
[2020-09-07] => Array
(
[total_stickers] => 40
[total_orders] => 28
[total_value] => 145.85
)
)
https://3v4l.org/qQ04D
Original answer: Sum all marketplaces.
<?php
$result = [];
foreach ($data as $date => $marketplaces) {
foreach ($marketplaces as $marketplace => $data) {
if (!isset($result[$marketplace])) {
$result[$marketplace] = $data;
} else {
$result[$marketplace] = [
'total_stickers' => $result[$marketplace]['total_stickers'] + $data['total_stickers'],
'total_orders' => $result[$marketplace]['total_orders'] + $data['total_orders'],
'total_value' => $result[$marketplace]['total_value'] + $data['total_value'],
];
}
}
}
print_r($result);
Result:
Array
(
[Etsy] => Array
(
[total_stickers] => 11
[total_orders] => 6
[total_value] => 42.75
)
[Woo] => Array
(
[total_stickers] => 29
[total_orders] => 13
[total_value] => 153.28
)
[eBay] => Array
(
[total_stickers] => 41
[total_orders] => 38
[total_value] => 125.39
)
)
https://3v4l.org/g4CmZ

how to sum 2 specific field of array if id was same in php

Guys I want to sum two array fields liked_cnt , coupon_cnt if first field id is same
how can I do this?
I did this but its toooooo slow
if (end($like)['id'] > end($coupon)['id']) {
$i = end($like)['id'];
$j = end($coupon)['id'];
}else {
$i = end($coupon)['id'];
$j = end($like)['id'];
}
for ($k=0; $k <= $i; $k++) {
for ($l=0; $l < $j; $l++) {
if ($like['id'] == $coupon['id']) {
$score[$like['id']] = ($coupon['coupon_cnt'] * 1000) + $like['liked_cnt'];
}else {
$score[$i] = 0;
}
}
}
//first array $Like
Array ( [0] => Array ( [id] => 85 [liked_cnt] => 6 ) [1] => Array ( [id] => 86 [liked_cnt] => 14 ) [2] => Array ( [id] => 92 [liked_cnt] => 6 ) [3] => Array ( [id] => 93 [liked_cnt] => 6 )
//second array $coupon
Array ( [0] => Array ( [id] => 35 [coupon_cnt] => 2 ) [1] => Array ( [id] => 86 [coupon_cnt] => 1 ) [2] => Array ( [id] => 139 [coupon_cnt] => 1 ) )
There is no magic algorithm builtin to php that can really compute your desired result in a much more efficient way. This might be slightly more efficient, but most of all it is more readable:
<?php
$input = [
[
['id' => 85, 'liked_cnt' => 6],
['id' => 86, 'liked_cnt' => 14],
['id' => 92, 'liked_cnt' => 6],
['id' => 93, 'liked_cnt' => 6]
],
[
['id' => 35, 'coupon_cnt' => 2],
['id' => 86, 'coupon_cnt' => 1],
['id' => 139, 'coupon_cnt' => 1]
]
];
$output = [];
foreach ($input as $set) {
array_walk($set, function($entry) use (&$output) {
$count = array_pop($entry);
$id = array_pop($entry);
if (array_key_exists($id, $output)) {
$output[$id]['total_cnt'] += $count;
} else {
$output[$id] = ['id' => $id, 'total_cnt' => $count];
}
});
}
print_r(array_values($output));
The output obviously is:
Array
(
[0] => Array
(
[id] => 85
[total_cnt] => 6
)
[1] => Array
(
[id] => 86
[total_cnt] => 15
)
[2] => Array
(
[id] => 92
[total_cnt] => 6
)
[3] => Array
(
[id] => 93
[total_cnt] => 6
)
[4] => Array
(
[id] => 35
[total_cnt] => 2
)
[5] => Array
(
[id] => 139
[total_cnt] => 1
)
)
UPDATE:
Considering your third comment below I add this modified version introducing the general multiplication of the coupon_cnt attribute by a factor 1000:
<?php
$input = [
[
['id' => 85, 'liked_cnt' => 6],
['id' => 86, 'liked_cnt' => 14],
['id' => 92, 'liked_cnt' => 6],
['id' => 93, 'liked_cnt' => 6]
],
[
['id' => 35, 'coupon_cnt' => 2],
['id' => 86, 'coupon_cnt' => 1],
['id' => 139, 'coupon_cnt' => 1],
['id' => 99, 'coupon_cnt' => 99]
]
];
$output = [];
foreach ($input as $set) {
array_walk($set, function($entry) use (&$output) {
$count = array_key_exists('coupon_cnt', $entry)
? 1000 * array_pop($entry)
: array_pop($entry);
$id = array_pop($entry);
if (array_key_exists($id, $output)) {
$output[$id]['total_cnt'] += $count;
} else {
$output[$id] = ['id' => $id, 'total_cnt' => $count];
}
});
}
print_r(array_values($output));

Sum arrays values by week number

Can you sum the below array of sales by weeknumber
Array
(
[0] => Array
(
[DATE] => 2015-03-06
[store] => 18
[weeknum] => 11
[sales] => 10
)
[1] => Array
(
[DATE] => 2015-03-08
[store] => 18
[weeknum] => 11
[sales] => 5
)
[2] => Array
(
[DATE] => 2015-03-09
[store] => 18
[weeknum] => 11
[sales] => 5
)
I would like to achieve something like this
[0] => Array
(
[store] => 18
[weeknum] => 11
[sales] => 20
)
so far i have tried array sum but that doesnt seem to work
There are many different ways and answers (eg.).
For instance:
Using Array Reduce:
$total_sales = array_reduce($items, function($carry, $item){
$carry['store'] = $item['store'];
$carry['weeknum'] = $item['weeknum'];
$carry['sales'] += $item['sales'];
return $carry;
}, []);
Or Using Array column:
$total_sales = array_sum(array_column($items, 'sales'));
Simply iterate through the array and add the field values to a sum container.
I've also added the store number as a unique identifier in case there's multiple stores you want individual info for. If you want it for all stores just change $key = $weekNumber . $item['store']; out with $key = $weekNumber;.
Data:
$items = [
[
'DATE' => '2015-03-06',
'store' => 18,
'weeknum' => 11,
'sales' => 10,
],
[
'DATE' => '2015-03-08',
'store' => 18,
'weeknum' => 11,
'sales' => 5,
],
[
'DATE' => '2015-03-09',
'store' => 18,
'weeknum' => 11,
'sales' => 5,
],
[
'DATE' => '2015-03-09',
'store' => 18,
'weeknum' => 12,
'sales' => 5,
],
];
Code:
<?php
$storeWeekSums = [];
foreach ($items as $item) {
// Save the key to refer to it later
$weekNumber = $item['weeknum'];
$key = $weekNumber . $item['store'];
if (!isset($weekSums[$weekNumber])) {
// The week and store does not already exist, let's create it with a value
$storeWeekSums[$key] = [
'store' => $item['store'],
'weeknum' => $item['weeknum'],
'sales' => $item['sales'],
];
} else {
// The week and store already exists, so we'll add to the current value instead
$storeWeekSums[$key]['sales'] += $item['sales'];
}
}
print_r($storeWeekSums);
Output::
Array
(
[1118] => Array
(
[store] => 18
[weeknum] => 11
[sales] => 5
)
[1218] => Array
(
[store] => 18
[weeknum] => 12
[sales] => 5
)
)
DEMO

Remove element from array in php

I am new to php and i want to remove element from array Here is my array:
Array
(
[Total] => 21600000
[Items] => Array
(
[2-13] => Array
(
[Item] => 2
[PID] => 13
[UPrice] => 11000000
[Qty] => 1
[Total] => 11000000
)
[58-167] => Array
(
[Item] => 58
[PID] => 167
[UPrice] => 5300000
[Qty] => 1
[Total] => 5300000
)
)
)
And i want to remove array element by PID.
I have try this but no luck:-
$ShoppingBag =$_SESSION['ssss'];
if ($ShoppingBag !== null && $ShoppingBag['Total'] > 0) {
foreach ($ShoppingBag['Items'] as $IOrder) {
if($IOrder["PID"]==13)
{
unset($ShoppingBag[$IOrder]);
}else
{
}
}
}
Please help. Thanks
You can try with one simple array map :)
$arr = [
'Total' => 21600000,
'Items' => [
'2-13' => [
'Item' => 2,
'PID' => 13,
'UPrice' => 11000000,
'Qty' => 1,
'Total' => 11000000
],
'58-167'=> [
'Item' => 58,
'PID' => 167,
'UPrice' => 5300000,
'Qty' => 1,
'Total' => 5300000
]
]
];
$test = array_map(function($ar) {
foreach($ar as $k=>$i) {
if( isset($i['PID']) && $i['PID'] == '13')
unset($ar[$k]);
}
return $ar; } , $arr);
var_dump($test);
You need 2 loop to do the action you want.
foreach($my_array as $key=>$value)
{
if(is_array($value))
{
foreach($value as $k=>$v)
{
if($k == 'PID')
{
unset($value[$k]);
}
}
}
}
with this you can remove only element with key PID.
Hi youre unsetting the $IOrder instead of the Item that you want to delete:
This code is a solution an i tested it :
$ShoppingBag = Array
(
"Total" => 21600000,
"Items" => Array
(
"2-13" => Array
(
"Item" => 2,
"PID" => 13,
"UPrice" => 11000000,
"Qty" => 1,
"Total" => 11000000,
),
"58-167" => Array
(
"Item" => 58,
"PID" => 167,
"UPrice" => 5300000,
"Qty" => 1,
"Total" => 5300000,
),
),
);
foreach($ShoppingBag["Items"] as $key => $value){
if($value["PID"]==13){
unset($ShoppingBag["Items"][$key]);
}
}
You should know that always when you're using foreach loop the foreach( $a as $b ) when you do something to $b , $a remains the same because tey are different variables :)
Hope it will help you .
Regards.
$arr = [
'Total' => 21600000,
'Items' => [
'2-13' => [
'Item' => 2,
'PID' => 13,
'UPrice' => 11000000,
'Qty' => 1,
'Total' => 11000000
],
'58-167'=> [
'Item' => 58,
'PID' => 167,
'UPrice' => 5300000,
'Qty' => 1,
'Total' => 5300000
]
]
];
$pid_to_remove = 13;
$new_ar = array_filter(
$arr,
function ($v) using ($pid_to_remove) {
return (!isset($v['PID'])) || ($v['PID'] != $pid_to_remove);
}
);

Categories