I have a large array of booking dates and amount, like so :
Array
(
[0] => Array
(
[date] => 2014-04-04
[total] => 30.00
)
[1] => Array
(
[date] => 2014-04-05
[total] => 47.00
)
[9998] => Array
(
[date] => 2014-08-21
[total] => 52.00
)
... ++ a lot of dates associated to numbers.
and process it to get monthly reports :
$months = array();
foreach($myarray as $k=>$v) {
list($y,$m) = explode("-",$v['date']);
$months[$y."-".$m][] = $v['total'];
}
which gives me a nice array :
Array
(
[2014-04] => Array <-- Every amount made in April
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
...
)
[2014-05] => Array <-- Every amount made in May
(
[0] => 68.00
[1] => 42.00
....
)...
However I'm trying to find a way the same thing but starting from a specific day, say 5 for example, in order to get (dates labelled for clarity):
Array
(
[from January 5 to February 4] => Array
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
)
[from February 5 to March 4] => Array
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
)...
So every amount made each month, but starting from the 5th of each month.
Something like this should work. I hate saying should, but I don't have the data to test with. Let me know if anything's wrong:
$months = array();
foreach($months as $k => $v) {
list($y, $m, $d) = explode("-", $v['date']);
if($d < 5) {
if($m == 1) {
$y--;
$m = 12;
} else {
$m--;
}
}
$months[$y . "-" . $m][] = $v['total'];
}
Related
I need to buildup a graph with total sales for each day for last week, I did not get all 7 days data, rather getting 3 days data from database, I need to push rest of the 4days with total sales 0,
I have the following array of 3 days
Array
(
[0] => Array
(
[SalesDate] => Jun09
[total] => 4
)
[1] => Array
(
[SalesDate] => Jun11
[total] => 2
)
[2] => Array
(
[SalesDate] => Jun14
[total] => 1
)
)
but I need all 7days data from Jun09 to Jun15 like this
Array
(
[0] => Array
(
[SalesDate] => Jun09
[total] => 4
)
[1] => Array
(
[SalesDate] => Jun10
[total] => 0
)
[2] => Array
(
[SalesDate] => Jun11
[total] => 2
)
[3] => Array
(
[SalesDate] => Jun12
[total] => 0
)
[4] => Array
(
[SalesDate] => Jun13
[total] => 0
)
[5] => Array
(
[SalesDate] => Jun14
[total] => 1
)
[6] => Array
(
[SalesDate] => Jun15
[total] => 0
)
)
I have tried with following code, but not getting the required data.
<?php
$sales =array(array('SalesDate' => 'Jun09',
'total' => 4
),
array
(
'SalesDate' => 'Jun11',
'total' => 2
),
array
(
'SalesDate' => 'Jun14',
'total' => 1
)
);
$final_array = array();
foreach($sales as $sale)
{
for($i=7;$i>0;$i--)
{
$current_weeks =[];
if($sale['SalesDate'] ==date('Md', strtotime("-".$i." days")))
{
$week_days['SalesDate'] = $sale['SalesDate'];
$week_days['total'] = $sale['total'];
}
else
{
$week_days['SalesDate'] = date('Md', strtotime("-".$i." days"));
$week_days['total'] = 0;
}
$final_array[] =$week_days;
}
}
You could first create a 'skeleton' array that matches your source array $arr, but with all the previous seven days $prevSevenDays. The date format matches the source array's, the totals are all set to 0.
// build empty skeleton
$prevSevenDays = [];
for ($i = 7; $i > 0; $i--) {
$prevSevenDays[$i]['SalesDate'] = date('Md', strtotime("-$i days"));
$prevSevenDays[$i]['Total'] = 0;
}
All you have to do is cycle through this array to replace the entries with available data in your source array $arr.
foreach ($arr as $sales) {
foreach ($prevSevenDays as $key => $day) {
if ($sales['SalesDate'] === $day['SalesDate']) $prevSevenDays[$key]['Total'] = $sales['Total'];
}
}
demo
I am fetching data from database which have dates that need to be sent to the view to create a chart. Some months might not have transactions which I need them to be autofilled with 0.
Model
function get_chart_data()
{
$this->db->order_by('month','asc');
$this->db->select('COUNT(*) as no_payments, SUM(amount) as total_payment_amount, YEAR(`date_paid`) AS year, MONTH(`date_paid`) AS month');
$this->db->group_by(array("year", "month"));
$this->db->where('tbl_payments.payment_type', "PAYMENT");
return $this->db->get('tbl_payments');
}
When I print_r in my controller the data is
Array ( [0] => stdClass Object ( [no_payments] => 1 [total_payment_amount] => 450 [year] => 2016 [month] => 1 ) [1] => stdClass Object ( [no_payments] => 5 [total_payment_amount] => 1162 [year] => 2016 [month] => 5 ) [2] => stdClass Object ( [no_payments] => 2 [total_payment_amount] => 1700 [year] => 2016 [month] => 6 ) )
How can I fill in the missing months and data with zero i.e the data to be
Array ( [0] => stdClass Object ( [no_payments] => 1 [total_payment_amount] => 450 [year] => 2016 [month] => 1 ) [1] => stdClass Object ( [no_payments] => 0 [total_payment_amount] => 0 [year] => 2016 [month] => 2 ) [2] => stdClass Object ( [no_payments] => 0 [total_payment_amount] => 0 [year] => 2016 [month] => 3 ) )
You could add them manually:
$months = array();
$query = $this->db->get('tbl_payments')->result();
foreach($query as $q)
{
$months[] = $q->month;
}
for($i = 1; $i <= 12; $i++)
{
if(!in_array($i, $months))
{
$new_data = new stdClass();
$new_data->no_payments = 0;
$new_data->total_payment_amount = 0;
$new_data->year = 2016;
$new_data->month = $i;
$query[] = $new_data;
}
}
That is asuming you will only need the filling months for current year. If you query other years, you may add that to the logic and include it in another loop.
I have to build a function which calculates the room options that are available for a guest.
For example; the guest has chosen 2 rooms in his inquiry, 1 room for ‘two adults’ and 1 room for ‘2 adults and a child’.
After a couple of calculations there will be 2 arrays. 1 array contains the possible rooms (searched per room inquiry) and 1 array has the availability (how many rooms are available of this room-type in the hotel).
The first array with the possible rooms will for example look like this:
$rooms = array
(
'adultsroom' => array(15,501,539,18),
'familyroom' => array(15,501,539)
);
echo '<pre>'; print_r($rooms); echo '</pre>';
Where nested array [0] stands for the first searched room inquiry (2 adults) and nested array [1] for the second searched room inquiry (2 adults and a 3 year old child).
So for the first room inquiry the possible room_id’s are '15', '501', '539' en '18'.
The second array with the availability will for example look like this:
(room_id’s that are mentioned in the first array will always appear in the availability array)
$roomsavailable = array
(
'15' => 1,
'501' => 2,
'539' => 1,
'18' => 4
);
echo '<pre>'; print_r($roomsavailable); echo '</pre>';
This array indicates that for the chosen arrival date there are 4 rooms available of room_id ‘18’.
The function called 'get_room_combinations' receives these array’s and calculates all the possible room combinations.
The array output will look in this case like this (where [0] in the nested array (2nd
level array) stands for the first room inquiry and [1] for the second room inquiry):
function get_room_combinations(array $data, array &$all = array(), array $group = array(), $value = null, $i = 0)
{
$keys = array_keys($data);
if (isset($value) === true) {
array_push($group, $value);
}
if ($i >= count($data)) {
array_push($all, $group);
} else {
$currentKey = $keys[$i];
$currentElement = $data[$currentKey];
foreach ($currentElement as $val) {
get_room_combinations($data, $all, $group, $val, $i + 1);
}
}
return $all;
}
$data = array(
array('15', '501', '539', '18'),
array('15', '501', '539'),
);
$combos = get_room_combinations($data);
echo '<pre>'; print_r($combos); echo '</pre>';
I have a piece of code which render one array with 12 combinations. It didn't look at the availability at the moment.
Who can help me here with this script to render one array with the 10 available combinations?
Array
(
[0] => Array
(
[0] => 15
[1] => 501
)
[1] => Array
(
[0] => 15
[1] => 539
)
[2] => Array
(
[0] => 501
[1] => 15
)
[3] => Array
(
[0] => 501
[1] => 501
)
[4] => Array
(
[0] => 501
[1] => 539
)
[5] => Array
(
[0] => 539
[1] => 15
)
[6] => Array
(
[0] => 539
[1] => 501
)
[7] => Array
(
[0] => 18
[1] => 15
)
[8] => Array
(
[0] => 18
[1] => 501
)
[9] => Array
(
[0] => 18
[1] => 539
)
);
How can i turn this array:
Array ( [0] => 80 ) Array ( [0] => 20 ) Array ( [0] => 90 )
Into such array:
Array (
[0] => 80,
[1] => 20,
[2] => 90
);
Code:
$percentage_result = $percentage_query->result_array(); //output below:
Output:
Array
(
[0] => Array
(
[id] => 62
[list_id] => 55
[start_date] => 1459987200
[end_date] => 1459987200
[percentage] => 80
)
[1] => Array
(
[id] => 64
[list_id] => 55
[start_date] => 1459814400
[end_date] => 1459814400
[percentage] => 20
)
[2] => Array
(
[id] => 63
[list_id] => 55
[start_date] => 1459900800
[end_date] => 1459900800
[percentage] => 90
)
I want to save all of the [percentage] and get the highest one.
Doing this:
$null = array();
foreach ($percentage_result as $ptime) {
//Days between start date and end date -> seasonal price
$start_time = $ptime['start_date'];
$end_time = $ptime['end_date'];
$percentage_sm = explode(',', $ptime['percentage']);
$mrg = array_merge($null, $percentage_sm);
print_r($mrg);
$msg shows me:
Array
(
[0] => 80
)
Array
(
[0] => 20
)
Array
(
[0] => 90
)
You can do this in very simple way like this
$percentage_sm = array(); //define blank array
foreach ($percentage_result as $ptime) {
//Days between start date and end date -> seasonal price
$start_time = $ptime['start_date'];
$end_time = $ptime['end_date'];
$percentage_sm[] = $ptime['percentage']; //assign every value to array
}
print_r($percentage_sm);
Use array_merge()
$result = array_merge($arr1, $arr2, $arr3);
print_r($result);
If you want to get the highest percentage value from your $percentage_result array, then the easiest way to do it is
$maxPercentage = max(array_column($percentage_result, 'percentage'));
rather than trying to do something weird with array_merge
(PHP >= 5.5.0)
If you're running a lower version of PHP, then you can do something similar with
$maxPercentage = max(
array_map(
$percentage_result,
function ($value) { return $value['percentage']; }
)
);
I have an array that output's this:
Array (
[0] => Array ( [week] => 1 )
[1] => Array ( [user] => 1 )
[2] => Array ( [score] => 6 )
[3] => Array ( [week] => 1 )
[4] => Array ( [user] => 15 )
[5] => Array ( [score] => 6 )
[6] => Array ( [week] => 2 )
[7] => Array ( [user] => 1 )
[8] => Array ( [score] => 5 )
[9] => Array ( [week] => 2 )
[10] => Array ( [user] => 15 )
[11] => Array ( [score] => 7 )
How do I drop the lowest score for user 1 (which would be 5 taken in week 2) from the array? I only want to drop the lowest number from the array for user 1 and then want to do the same thing for user 15 (which would be 6 in week 1).
Then, how would I sum the remaining scores in the array by user? There will be more weeks data to follow and I always want to drop the lowest per user for any week.
So thus far the total score for User 1 would be 6 thru 2 weeks and the total score for User 15 would be 7 thru week 2. If week 3 has lower scores for both users then their totals would be: User 1 = 11 and User 15 = 13.
Thanks in advance!!
Assuming that all records are in groups of 3:
$array = array(/* your data */);
$count = count($array) / 3;
$lowest = 0;
$min = PHP_INT_MAX;
for($i = 0; $i < $count; $i++)
{
if($array[$i + 1]['user'] == 1)
{
if($array[$i + 2]['score'] < $min)
{
$min = $array[$i + 2]['score'];
$lowest = $i;
}
}
}
unset($array[$lowest]);
unset($array[$lowest + 1]);
unset($array[$lowest + 2]);
Should at least give you the idea.
Your array structure is bizarre. Why is every array element itself a singleton array? Something like the following would make more sense:
$array_better = array (
[0] => array (
['week'] => 1,
['user'] => 1,
['score'] => 6
),
[1] => array (
['week'] => 1,
['user'] => 15,
['score'] => 6
),
[2] => array (
['week'] => 2,
['user'] => 1,
['score'] => 5
),
[3] => array (
['week'] => 2,
['user'] => 15,
['score'] => 7
)
)
But for that matter, what would make still more sense would be a $array[$week][$user] structure, like this:
$array_wu = array (
[1] => array (
[1] => 6,
[15] => 6
),
[2] => array (
[1] => 5,
[15] => 7
)
)
(If you do not understand what I have done here, please ask me about it.)
Alternatively again, you could have a $array[$user][$week] structure:
$array_uw = array (
[1] => array (
[1] => 6,
[2] => 5
),
[15] => array (
[1] => 6,
[2] => 7
)
)
This in particular would make it very easy to do what you are trying to do - in fact it would take only one line of code statement:
$sum_excl_lowest = (array_key_exists(1, $array_uw) and count($array_uw[1])) ?
array_sum($array_uw[1]) - min($array_uw[1]) :
0;