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']; }
)
);
Related
I have an array like this below,
Array
(
[2] => Array
(
[id] => 75
[program] => Apr 2020-Nov 2020
)
[1] => Array
(
[id] => 73
[program] => Feb 2016-Aug 2020
)
[0] => Array
(
[id] => 72
[program] => May 2020-Dec 2020
)
)
The resultant array should be
Array
(
[1] => Array
(
[id] => 73
[current_program] => Feb 2016-Aug 2020
)
[2] => Array
(
[id] => 75
[current_program] => Apr 2020-Nov 2020
)
[0] => Array
(
[id] => 72
[current_program] => May 2020-Dec 2020
)
)
It should sort based on the year. I have tried to achieve by "strnatcasecmp", but the array is sorting by alphabet not by the numeric value in it
usort($programp, function($a, $b) {
return strnatcasecmp($a['program'], $b['program']);
});
Any help would be appreciated!
Thanks
You need to convert the first month and year into a timestamp, sort that thereby sorting the original:
foreach($programp as $values) {
$starts[] = strtotime(explode('-', $values['program'])[0]);
}
array_multisort($starts, $programp);
Before sorting the $starts array would look like this, easy to sort:
Array
(
[0] => 1585692000
[1] => 1454281200
[2] => 1588284000
)
You probably want some error checking to make sure $values['program'] is not empty etc...
//Sort indexes so the keys are 0,1,2 instead of 1,2,0
//This is important when sorting down below with asort()
$arr = array_values($arr);
//Go through every "program" (dateinterval in your array)
//and make a new array in the format year-monthnr
//Date parse returns the number of jan(1),feb(2),mar(3) etc..
$year_month = [];
foreach(array_column($arr,'program') as $key => $item) {
$year = explode(' ', explode('-',$item)[0])[1];
$month = date_parse(explode(' ', explode('-',$item)[0])[0])['month'];
$year_month[] = $year . '-' . $month;
}
//Sort with mainted indexes (keys)
asort($year_month);
//Create new array "mapped" keys to the original array $arr
$new_arr = [];
foreach($year_month as $key=>$item) {
$new_arr[] = $arr[$key];
}
Output of $new_arr would be:
Array
(
[0] => Array
(
[id] => 73
[program] => Feb 2016-Aug 2020
)
[1] => Array
(
[id] => 75
[program] => Apr 2020-Nov 2020
)
[2] => Array
(
[id] => 72
[program] => May 2020-Dec 2020
)
)
time is DateTime Object formatted as:
$time = $date->format("%H:%i:%s");
I'm trying to sum hours and group employees.
I have:
Array
(
[0] => Array
(
[employee] => 2
[time] => 00:0:17
)
[1] => Array
(
[employee] => 1
[time] => 00:0:4
)
[2] => Array
(
[employee] => 2
[time] => 02:0:0
)
[3] => Array
(
[employee] => 3
[time] => 02:0:0
)
[4] => Array
(
[employee] => 3
[time] => 01:0:0
)
)
And I need something like, grouped and sum:
Array
(
[0] => Array
(
[employee] => 2
[totalTime] => 02:0:17
)
[1] => Array
(
[employee] => 1
[totalTime] => 00:0:4
)
[2] => Array
(
[employee] => 3
[totalTime] => 03:0:0
)
)
Any thoughts? Maybe convert time to UNIX and manipulate from there?
Since you want something like like the desired output, perhaps this will do:
foreach($array as $value){
if(!isset($data[$value['employee']])){
$data[$value['employee']] = 0;
}
$parsed = date_parse($value['time']);
$data[$value['employee']] += $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
}
print_r($data);
Instead of creating a multidimensional array, $data will have the key of the employee and the value of the total time in seconds.
Obviously you can convert seconds very easily to a format if you so please.
I have an array of dates that looks like this:
Array
(
[0] => '2014-01-01'
[1] => '2014-01-02'
[2] => '2014-01-03'
[3] => '2014-01-04'
[4] => '2014-01-05'
[5] => '2014-01-06'
[6] => '2014-01-07'
)
and I have another array of dates and counts that looks like this:
[All] => Array
(
[0] => Array
(
[count] => 2
[date] => 2014-01-06
)
[1] => Array
(
[count] => 1
[date] => 2014-01-03
)
[2] => Array
(
[count] => 43
[date] => 2013-12-11
)
[3] => Array
(
[count] => 103
[date] => 2013-12-10
)
[4] => Array
(
[count] => 128
[date] => 2013-12-09
)
[5] => Array
(
[count] => 75
[date] => 2013-12-08
)
[6] => Array
(
[count] => 107
[date] => 2013-12-07
)
I want to make a new associative array where all the keys are the dates from the first array above and all of the values are either the count matched up with the corresponding date or "0".
So for instance, the new array would look like this:
Array
(
[2014-01-01] => 0
[2014-01-02] => 0
[2014-01-03] => 1
[2014-01-04] => 0
[2014-01-05] => 0
[2014-01-06] => 2
[2014-01-07] => 0
)
Does that make sense? Please feel free to ask any questions you may have. Thank you!
Try this code:
$result = array();
foreach($firstArray as $f){
foreach($secondArray as $s){
if($s['date'] == $f) $result[$f] = $s['count'];
}
if(!array_key_exists($f, $result)) $result[$f] = 0;
}
$result = array();
foreach($secondArray as $s){
if(in_array($s['date'], $firstArray) {
unset($firstArray[$s['date']]);
$result[$s['date']] = $s['count'];
}
}
// if items left in first array that are not found within foreach:
if (!empty($firstArray))
$result = array_merge($result, array_fill_keys($firstArray, 0));
// sort by key so dates go ascending
ksort($result);
$new = array();
foreach($all as $row)
{
$new[$row['date']] = $row['count'];
}
array_merge ($new, $old);
Here $all is the array with the date and count indices.
$old is the exisisting array.
That's a 2-liner:
// use dates as index and set everything to 0
$result = array_fill_keys($x, 0));
// copy over existing counts
array_walk($all, function($v) use (&$result) { if (array_key_exists($v['date'], $result)) { $result[$v['date']] = $v['count'];}});
I have an array, let call it $mainArray, which looks like this: -
Array
(
[1] => Array
(
)
[5] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
)
[80] => Array
(
[0] => 20
[1] => 40
[2] => 50
[3] => 60
)
[777] => Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)
[666] => Array
(
[0] => 1234
[1] => 5678
[2] => 20
[3] => 9865
)
[555] => Array
(
[0] => 111
[1] => 222
[2] => 333
[3] => 444
)
)
What I want to do is create 2 new arrays: -
1) Where values are equal to the key names of $mainArray, but only those where the sub-array (if there is one) contains the value "20" somewhere in it. For example my new array (call it $arrayOne) will be [0] => 5, [1] => 80, [2] => 666.
2) Similar to above, but where there's either no sub-array or, if there is, it doesn't include "20" as a value. So that (call it $arrayTwo) would be [0] => 1, [1] => 777, [2] =>555.
I've tried loads of for each loops and even a little RecursiveIteratorIterator (whatever that is!) but can't seem to reference keys and values in the way that I need to. Any help would be much appreciated!
Will this do?:
<?php
foreach( $mainArray as $mKey => &$mVal )
{
if( in_array( 20, $mVal ) )
{
$arrayOne[] = $mKey;
}
else
{
$arrayTwo[] = $mKey;
}
}
I trust you can create a function which would check if array contains 20 as it's value or not. Let's call this function has20.
You two new arrays would then be array_filter($mainArray, 'has20') and array_filter($mainArray, function ($x) {return !has20($x);})
You can do it like this:
$newArray = array();
foreach($mainArray as $key => $subArray) {
if (in_array(20, $subArray)) {
$newArray[] = $key;
}
}
new here so thanks for taking the time to read my question.
I am running some PHP code that compares the numbers enter on the screen with those in a database. The problem I am having is ordering the two dimensional array after manipulating each line. It looks as though the array id numbers are being removed. I would like to order the array by column [2] in descending order. Can anyone offer any help?
while( $a_row = mysql_fetch_array( $result))
{
$draw = array($a_row['Drawn1'],
$a_row['Drawn2'],
$a_row['Drawn3'],
$a_row['Drawn4'],
$a_row['Drawn5'],
$a_row['Drawn6'],
$a_row['Drawn7'],
$a_row['Drawn8']);
$numbers = array("6", "9", "4", "8", "14", "18");
if (count(array_intersect($draw, $numbers)) >= 1) {
$rs = array(($a_row['DrawNo']), join(" , ",array_intersect($draw, $numbers)), count(array_intersect($draw, $numbers)));
} else {
$rs = null;
}
array_multisort($rs[1], SORT_NUMERIC, SORT_DESC, $rs[0], SORT_ASC, SORT_STRING);
print_r ($rs);
echo "<br />";
}
This is what the output looks like.
Array ( [0] => A0048 [1] => 14 [2] => 1 )
Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 )
Array ( [0] => A0050 [1] => 14 [2] => 1 )
Array ( [0] => A0051 [1] => 14 [2] => 1 )
Array ( [0] => A0052 [1] => 18 [2] => 1 )
Array ( [0] => A0053 [1] => 6 , 14 [2] => 2 )
Array ( [0] => A0054 [1] => 6 [2] => 1 )
Array ( [0] => A0055 [1] => 14 [2] => 1 )
Array ( [0] => A0056 [1] => 4 [2] => 1 )
Array ( [0] => A0057 [1] => 9 , 6 , 4 [2] => 3 )
Thanks for your time
zeroanarchy
Numeric array keys being reindexed is part of the documented behavior of array_multisort(). If you need keys preserved, you need to convert them to string keys.
you are probably looking for:
http://php.net/manual/en/function.array-multisort.php