I have the following array :
Array
(
[0] => Array
(
[year_col] => 2016
[week_col] => 21
[total] => 1
)
[1] => Array
(
[year_col] => 2016
[week_col] => 22
[total] => 2
)
[2] => Array
(
[year_col] => 2016
[week_col] => 22
[total] => 2
)
[3] => Array
(
[year_col] => 2016
[week_col] => 22
[total] => 3
)
[4] => Array
(
[year_col] => 2016
[week_col] => 23
[total] => 2
)
)
I neet to get the sum of total for each week_col. In this case I want to get :
21 => 1, 22 => 7, 23 => 2
I try :
$sum = 0;
foreach($array as $draw){
$a_filter_draws[$draw['week_col']] = array(
'total_a' => $sum+=$draw['total']
);
}
Can you help me please ? It don't return the good sum. Thx in advance and sorry for my english
$a_filter_draws = array();
foreach($array as $draw){
if (!isset($a_filter_draws[$draw['week_col']])) {
$a_filter_draws[$draw['week_col']] = 0;
}
$a_filter_draws[$draw['week_col']] += $draw['total'];
}
Try this:
<?php
$arrSum = array();
foreach($array as $draw){
if(isset($arrSum[$draw['week_col']])) {
$arrSum[$draw['week_col']] += $draw['total'];
} else {
$arrSum[$draw['week_col']] = $draw['total'];
}
}
?>
<?php
$arr=Array
(Array
(
"year_col" => 2016,
"week_col" => 21,
"total" => 1
)
,Array
(
"year_col" => 2016,
"week_col" => 22,
"total" => 2
)
, Array
(
"year_col" => 2016,
"week_col" => 22,
"total" => 2
)
, Array
(
"year_col" => 2016,
"week_col" => 22,
"total" => 3
)
,Array
(
"year_col" => 2016,
"week_col" => 23,
"total" => 2
)
);
$sum = 0;
$total=0;
foreach($arr as $draw){
$sum+=$draw["week_col"];
$total+=$draw["total"];
}
echo $sum;
echo "<br>";
echo $total;
?>
Related
I want to get result as sum of total and group by month,
my array look like:
Array
(
[0] => Array
(
[order_no] => 222
[month] => Aug-22
[totalAmount] => 2305
)
[1] => Array
([order_no] => 333
[month] => Aug-22
[totalAmount] => 945
)
[2] => Array
(
[order_no] => 1
[month] => Sep-22
[totalAmount] => 945
)
[3] => Array
(
[order_no] => 111
[month] => Sep-22
[totalAmount] => 2305
)
)
What I am trying to do:
I want to group these data by MONTH and return the sum
Expected Result:
Array
(
[0] => Array
(
[month] => Aug-22
[totalAmount] => 3254
)
[1] => Array
(
[month] => Sep-22
[totalAmount] => 3254
)
)
This is classic problem to use array_reduce function:
<?php
$arr = [
["order_no" => 222, "month" => "Aug-22", "totalAmount" => 2305],
["order_no" => 333, "month" => "Aug-22", "totalAmount" => 945],
["order_no" => 1, "month" => "Sep-22", "totalAmount" => 945],
["order_no" => 111, "month" => "Sep-22", "totalAmount" => 2305],
];
$res = array_reduce(
$arr,
function($acc, $order) {
if (isset($acc[$order['month']])) {
$acc[$order['month']]['totalAmount'] += $order['totalAmount'];
} else {
$acc[$order['month']] = [
"month" => $order['month'], "totalAmount" => $order['totalAmount']
];
}
return $acc;
},
[]
);
print_r($res);
run php online
can you please help me out in this.
What will be the function to remove 4th and 5th array in PHP.
Array
(
[0] => Array
(
[10] => 98
[11] => 1
[433438] => 8
)
[1] => Array
(
[10] => 98
[11] => 1
[433438] => 1
)
[2] => Array
(
[13] => 98
[11] => 2
[433438] => 8
)
[3] => Array
(
[14] => 98
[11] => 2
[433438] => 1
)
[4] => Array
(
[10] => 18
[11] => 1
)
[5] => Array
(
[14] => 18
[11] => 2
)
)
Thanks in Advance.
$base = [
[10 => 98, 11 => 1, 433438 => 8],
[10 => 98, 11 => 1, 433438 => 1],
[13 => 98, 11 => 2, 433438 => 8],
[14 => 98, 11 => 2, 433438 => 1],
[10 => 18, 11 => 1],
[14 => 18, 11 => 2],
];
$invalid = [];
for ($i = 0; $i <= count($base) - 1; $i++) {
for ($j = 0; $j <= count($base) - 1; $j++) {
$refCount = count($base[$j]);
$interSectCount = count(array_intersect(array_keys($base[$i]), array_keys($base[$j])));
if (count($base[$i]) !== $refCount && $interSectCount === $refCount) {
$invalid[] = $j;
}
}
}
foreach ($invalid as $item) {
unset($base[$item]);
}
You can use array_pop() function which eliminates the last element of the array but if you use it two times you will get the desired result as
<?php
$data = [
1 => [1,2],
2 => [1,3],
3 => [1,4]
];
$value = array_pop($data);
$value = array_pop($data);
?>
Output
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
)
i have this array
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] => 3
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] => 9
[post_user_added_id] => 15
)
what i want to do is if the key post_id is repeated i just want to empty it and keep one so my final array will look like this
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] =>
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] =>
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] =>
[post_user_added_id] => 15
)
i have tried this code but it doesn't seem to work it deletes the whole array
foreach($arry as $k => $v)
{
foreach($arry as $key => $value)
{
if($k != $key && $v['post_id'] == $value['post_id'])
{
unset($arry [$k]);
}
}
}
print_r($arry);
You can perform foreach with ternary operator
$last = null;//this will keep the previous post_id
foreach($arr as &$v){
($last && $last == $v['post_id']) ? ($v['post_id'] = '') : ($last = $v['post_id']);
}
print_r($arr);
Working example :- https://3v4l.org/RiU9J
here try this.
$tempArr = [];
$resArr = [];
foreach($orignalArr as $key=>$obj){
if(in_array($obj['post_id'], $tempArr)){
$obj['post_id'] = '';
$resArr[] = $obj;
}else{
$tempArr[] = $obj['post_id'];
$resArr[] = $obj;
}
}
print_r($resArr);
try this :
// example code
$arrayResult = array();
$arry = [
1 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 2
],
2=>
[
'user_id' => 76,
'post_id' => 3,
'post_user_added_id' => 16
],
3 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 12
],
4 =>
[
'user_id' => 76,
'post_id' => 9,
'post_user_added_id' => 15
],
5 =>
[
'user_id' => 77,
'post_id' => 9,
'post_user_added_id' => 15
]];
$final = array();
foreach($arry as $a)
{
if (in_array($a['post_id'], $arrayResult)) {
$a['post_id'] = 0;
}else{
$arrayResult[] = $a['post_id'];
}
$final[] = $a;
}
var_dump($final);
Maybe if you use a stack for comparison?
edit: rewritten the code...
$stack=[];
foreach ($array as $index => $subarray){
if(in_array($subarray['post_id'], $stack)) {
$array[$index]['post_id'] = null;
}
else $stack[]=$subarray['post_id'];
}
print_r($array);
example: https://3v4l.org/FsPUG
Hope this helps!
use of your array value in instead of $arr and try to filter array like the following code :
$arr = array(
array(
'user_id'=>15,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
);
$postids = array();
foreach($arr as $key=>$val){
if(in_array($val['post_id'], $postids)){
$arr[$key]['post_id'] = '';
}else{
$postids[] = $val['post_id'];
}
}
echo "<pre>";print_r($arr);exit;
Output :
Array
(
[0] => Array
(
[user_id] => 15
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
[2] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[3] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[4] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
)
I want which is the biggest array from following array.
[13] => Array
(
[0] => 1
[1] => 3
[2] => 9
)
[15] => Array
(
[0] => 1
[1] => 5
[2] => 8
)
[33] => Array
(
[0] => 1
[1] => 9
[2] => 13
)
I want a code that would return last array with key 33.
Please Help.
Use max to get the maximum from the keys of your array
$yourarray=array(13 => Array
(
0 => 1,
1 => 3,
2 => 9,
),
15 => Array
(
0 => 1,
1 => 5,
2 => 8,
),
33 => Array
(
0 => 1,
1 => 9,
2 => 13,
));
$arr=max(array_keys($yourarray));
print_r($yourarray[$arr]);
Output:
Array
(
[0] => 1
[1] => 9
[2] => 13
)
This should do the trick...
<?php
$tests = array(
13 => array(1,3,9),
15 => array(1,5,8),
33 => array(1,9,13)
);
$array_totals = array_map('array_sum', $tests);
krsort($array_totals);
$maxArray = each($array_totals);
var_dump($maxArray);
Gives
array (size=4)
1 => int 23
'value' => int 23
0 => int 33
'key' => int 33
Not the most beautiful thing, but readable ;)
$tests = array(
13 => array(1,3,9),
15 => array(1,5,8),
33 => array(1,9,13)
);
$max = -1;
$max_key = -1;
foreach ($tests as $k => $v) {
$cur_max = max($v);
if ($cur_max >= $max) {
$max = $cur_max;
$max_key = $k;
}
}
echo "max: $max; max_key: $max_key";
Gives:
max: 13; max_key: 33
To make it more beautiful: use array_map and sorting.
I have this code that outputs something like:
Array (
[0] => 15
[1] => 13
[2] => 16
[3] => 16
[4] => 10
[5] => 10
[6] => 13
[7] => 13
)
But, i want this structure:
Array (
[0] => Array ( [0] => 15, [1] => 13, [2] => 16, [3] => 16)
[0] => Array ( [0] => 10, [1] => 10, [2] => 13, [3] => 13)
)
This didn't solve : $score[] = array($score_bd);. Any idea ?
php code
$i =0;
foreach ($arr_user_apply as $val) {
$new_val = array($val);
$arr[$i] = array_merge($str, $new_arr_tags_ids, $new_val, $new_id_oferta);
$sql = $db -> prepare("
query
"
);
call_user_func_array(array($sql, "bind_param"), $arr[$i]);
$sql -> execute();
$sql -> bind_result($score_bd);
while ($sql -> fetch()) {
$score[] = $score_bd;
};
$i++;
}
If you're just trying to add another dimension to your first array by placing the key value of the key into the new array something like this might work:
$firstArray = Array ( 0 => 15, 1 => 15, 2 => 13, 3 => 13, 4 => 16, 5 => 16, 6 => 10, 7 => 10, 8 => 13, 9 => 13, 10 => 6, 11 => 6 );
$newArray = Array();
foreach($firstArray as $key => $value)
{
$newArray[] = Array($key => Array($value));
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
EDIT:
if that's one dimension too deep, then just try this:
$firstArray = Array ( 0 => 15, 1 => 15, 2 => 13, 3 => 13, 4 => 16, 5 => 16, 6 => 10, 7 => 10, 8 => 13, 9 => 13, 10 => 6, 11 => 6 );
$newArray = Array();
foreach($firstArray as $key => $value)
{
$newArray[$key] = Array($value);
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
$arr = array ( 0 => 15 ,1 => 15, 2 => 13, 3 => 13 ,4 => 16 ,5 => 16 ,6 => 10, 7 => 10);
foreach($arr as $key=>$value){
$new_arr[$key][0] = $value;
}
i am not sure if the question is clear, but this works properly to me
$score = array();
while ($sql -> fetch()) {
$score[] = $score_bd;
};
$new_score[] = array($score);
Array (
[0] => Array ( [0] => Array ( [0] => 15 [1] => 13 [2] => 16 ) )
[1] => Array ( [0] => Array ( [0] => 10 [1] => 13 [2] => 6 ) )
)