Count subarray elements and sum deep subarray elements - php

I'm working with an API that returns a JSON object which I then decode into an array using
I need to get a count of all the [data] items in the array, and then generate a total of all the [Total] values contained in each
Here's my array:-
Array
(
[errorCode] => 0
[result] => OK
[data] => Array
(
[0] => Array
(
[fieldData] => Array
(
[Due Date] => 11/30/2017
[Date] => 11/30/2017
[Total] => 128.97
[Customers::Company] => A B C Lock & Key
[Status] => Paid
[Date Payment] => 11/30/2017
)
[portalData] => Array
(
)
[recordId] => 1
[modId] => 4
)
[1] => Array
(
[fieldData] => Array
(
[Due Date] => 12/01/2017
[Date] => 12/01/2017
[Total] => 256
[Customers::Company] => Kim Peacock Beringhause
[Status] => Paid
[Date Payment] => 12/01/2017
)
[portalData] => Array
(
)
[recordId] => 2
[modId] => 3
)
[2] => Array
(
[fieldData] => Array
(
[Due Date] => 11/30/2017
[Date] => 11/30/2017
[Total] => 1880
[Customers::Company] => Norton, Robert L Esq
[Status] => Unpaid Overdue
[Date Payment] =>
)
[portalData] => Array
(
)
[recordId] => 3
[modId] => 0
)
[3] => Array
(
[fieldData] => Array
(
[Due Date] => 12/22/2017
[Date] => 12/22/2017
[Total] => 1278
[Customers::Company] => Shapiro, Mark R Esq
[Status] => Unpaid
[Date Payment] =>
)
[portalData] => Array
(
)
[recordId] => 10
[modId] => 1
)
)
)
I've tried:
count($array)
which returns 3 which I can see are the count of the 3 items in that parent array (errorCode, result and data), but I can't work out how to focus on the data only.
The count value I'm after is 4 and the total value is 3542.97.

For count you need to do:
count($array['data']);
And for getting sum of all Total index values:
echo array_sum(
array_column(
array_column(
$array['data'],
'fieldData'
),
'Total'
)
);
Reference:- PHP: array_column - Manual

Related

Sort a multidimensional an array with numeric keys but keep the keys same just change the order [duplicate]

This question already has an answer here:
PHP sort associative array by numeric key in asc order [duplicate]
(1 answer)
Closed 10 months ago.
So I have 3 dimensional array. I want that array to be reordered based on the keys but the value of the keys should remain as it is. Like for an example if the array keys are 5,2,4,1,3 then it should become 1,2,3,4,5. Below I'm providing the array I have and excepted array and the solutions I have tried.
This is the array I have :-
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
Following are the solutions I have tried :-
$result = ksort($result);
$result = array_values($result);
$result = array_splice($result, 0, 0);
$result = sort($result);
$result = array_splice($result, 0, count($result));
This is the expected array :-
Array
(
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
)
Nothing is working any help will be appreciated. thanks in advance.
You are using ksort as $result = ksort($result);, ksort return TRUE/FALSE. That means you are assigning that to $results.
Read here PHP ksort
Your code should be:-
ksort($results);
instead of
$result = ksort($result);
You can use ksort for the keys sorting, here is an example
$arr = [
5 => [1,3],
3 => [2,3],
2 => [0,7]
];
ksort($arr);
echo '<pre>';
print_r($arr);
Output
Array
(
[2] => Array
(
[0] => 0
[1] => 7
)
[3] => Array
(
[0] => 2
[1] => 3
)
[5] => Array
(
[0] => 1
[1] => 3
)
)

Compare values of two multidimentional array and insert if not exits

I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){

php call sphinx, total found is 2, but matches is empty, why?

php call sphinx, total found is 2, but matches is empty, why?
php package: https://pecl.php.net/package/sphinx version:1.3.3
sphinx version:2.0.1
when I query('#author tairyao', '*'), the result is:
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => author
[1] => title
[2] => description
)
[attrs] => Array
(
)
[matches] => Array
(
[102373] => Array
(
[weight] => 7702
[attrs] => Array
(
)
)
[102428] => Array
(
[weight] => 7702
[attrs] => Array
(
)
)
)
[total] => 2
[total_found] => 2
[time] => 0
[words] => Array
(
[tairyao] => Array
(
[docs] => 4
[hits] => 4
)
)
)
The total and total_found are all 2, why attrs is empty?

PHP: move values of array under others keys

I have this array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -5,7
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -7
[negative] => Array
(
[0] => Array
(
[amount] => -7
)
)
)
)
You can note that key 0 has +10.00 of positive and -5.7 of negative (they are money transactions).
Key 1 has +13 and -7.
Basically, I need to iterate into array and move 4.30 under key 0, taken from THE NEGATIVE of key 1.
This is must be the final array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -10.00
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
[3] => Array
(
[amount] => -4.30
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -2.70
[negative] => Array
(
[0] => Array
(
[amount] => -2.70
)
)
)
)
If you need code that performs the transformation on this particular array (where you know the array indexes), the following will do what you want:
$expense = -4.30;
$your_array[0]['negative'][] = ['amount' => $expense];
$your_array[0]['negative_sum'] += $expense;
$your_array[1]['negative'][0]['amount'] -= $expense;
$your_array[1]['negative_sum'] -= $expense;
If you want a more "general" approach for arbitrary indexes and arbitrary amount entries, you will need to rephrase your question in broader terms.

Calculate Same date values from dimensional array

I have Following Array
$my_array = Array ( [0] => Array ( [date] => 2014-03-25 [total] => 649 ) [1] => Array ( [date] => 2014-03-29 [total] => 415.36 ) [2] => Array ( [date] => 2014-03-29 [total] => 29181.75 ) [3] => Array ( [date] => 2014-03-29 [total] => 1577.88 ) [4] => Array ( [date] => 2014-03-31 [total] => 849.4 ) [5] => Array ( [date] => 2014-04-11 [total] => 21382.2 ) [6] => Array ( [date] => 2014-04-16 [total] => 45022.5 ) )
$my_array in foreach output inner arrays like this.
Array ( [date] => 2014-03-25 [total] => 649 )
Array ( [date] => 2014-03-29 [total] => 415.36 )
Array ( [date] => 2014-03-29 [total] => 29181.75 )
Array ( [date] => 2014-03-29 [total] => 1577.88 )
Array ( [date] => 2014-03-31 [total] => 849.4 )
Array ( [date] => 2014-04-11 [total] => 21382.2 )
Array ( [date] => 2014-04-16 [total] => 45022.5 )
There are 3 sales for date 29 , I am trying to get Total sales of each day which should output.
Date 1 total sales
date2 total sales
date 3 total sales (if 3 sales their total)
can anyone help me please?
You could achieve this using array_walk and by implementing this call-back function.
<?php
$my_array = Array ( '0' => Array ( 'date' => '2014-03-25' ,'total' => 649 ), '1' => Array ( 'date' => '2014-03-29' ,'total' => 415.36 ), '2' => Array ( 'date' => '2014-03-29' ,'total' => 29181.75 ), '3' => Array ( 'date' => '2014-03-29', 'total' => 1577.88 ) ,'4' => Array ( 'date' => '2014-03-31', 'total' => 849.4 ), '5' => Array ( 'date' => '2014-04-11', 'total' => 21382.2 ) ,'6' => Array ( 'date' => '2014-04-16', 'total' => 45022.5 ) );
$new_arr = array();
array_walk($my_array,function ($v,$k) use(&$new_arr)
{
array_key_exists($v['date'],$new_arr) ? $new_arr[$v['date']] = $new_arr[$v['date']]+$v['total'] : $new_arr[$v['date']]=$v['total'];
});
echo "<pre>";
print_r($new_arr);
OUTPUT :
Array
(
[2014-03-25] => 649
[2014-03-29] => 31174.99
[2014-03-31] => 849.4
[2014-04-11] => 21382.2
[2014-04-16] => 45022.5
)
As you can see , the total sales are the values and the dates are the keys. The total sales have been added up and is shown next to each dates.

Categories