I've have an array such as
//$ary, $ary contains some 80 entries, I'm showing only the first 10
Array
(
[0] => Array
(
[December 2012] => 58
)
[1] => Array
(
[November 2012] => 84
)
[2] => Array
(
[December 2012] => 83
)
[3] => Array
(
[November 2012] => 72
)
[4] => Array
(
[November 2012] => 47
)
[5] => Array
(
[December 2012] => 93
)
[6] => Array
(
[November 2012] => 79
)
[7] => Array
(
[October 2012] => 70
)
[8] => Array
(
[November 2012] => 75
)
[9] => Array
(
[October 2012] => 59
)
[10] => Array
(
[December 2012] => 67
)
)
I'm able to get the total for each month using:
foreach($ary as $array)
{
foreach($array as $month=>$cent)
{
if(isset($abc[$month])) // prevent index warning
{
$abc[$month] += $cent;
// tried using $abc[$month] = $abc[$month]/2 but wrong values were returned
}
else
{
$abc[$month] = $cent;
}
}
}
This is resulting in
Array
(
[December 2012] => 2195
[November 2012] => 2159
[October 2012] => 1631
)
But I'm unable to find a way to find the average value for each month. In my case, there are 31 instances of December 2012, 31 instances of November 2012 and 25 instances of October 2012. Thus, I need to get hold of these 31, 31, and 25 values so that I can divide a month by that number.
I'm thinking something must be done inside if(isset($abc[$month])) loop to capture but haven't been successful so far. Is there any other way to get the average?
PS : I can't hardcode the month's name (October, November, December) for comparison purposes. They keep changing at regular intervals.
$eachMonth = array();
foreach($ary as $array)
{
foreach($array as $month=>$cent)
{
$eachMonth[$month][] = $cent;
if(isset($abc[$month])) // prevent index warning
{
$abc[$month] += $cent
}
else
{
$abc[$month] = $cent;
}
}
}
then for each month you can calculate average:
foreach( $eachMonth as $month => $values)
{
echo $month.' : '.$abc[$month] / count($values);
}
try this :
$your_array = array(array("December 2012" => 58),
array("December 2012" => 58)
);
$res = array();
foreach($your_array as $val){
$res[key($val)][] = $val[key($val)];
}
foreach($res as $k=>&$v){
$v = array_sum($v)/count($v);
}
echo "<pre>";
print_r($res);
Related
I am trying to split an array of space-delimited strings, group by a particular column, then store the data within each group in a more convenient structure.
Sample data:
$dataArray = [
0 => "AAAAA 2023 01 25 01:04:00 ID:20fjrjeZZ",
1 => "AAAAA 2023 01 25 01:18:08 ID:13454B43A",
2 => "AAAAA 2023 01 25 02:00:02 ID:18f5hjeWe",
3 => "AAAAA 2023 01 25 04:10:13 ID:13454B43A",
4 => "BBBBB 2023 01 25 01:44:10 ID:Xj74320fj",
5 => "BBBBB 2023 01 25 07:08:58 ID:13454B43A",
6 => "BBBBB 2023 01 25 08:40:52 ID:Ftzkk800Y",
7 => "BBBBB 2023 01 25 14:10:13 ID:18f5hjeWe"
];
I split the rows on the space with:
$lines = explode(' ', $dataArray);
Then I want to push the individual parts (AAAA, 2023, 01, ...) into an array.
foreach($dataArray as $parts){
$spotArray[] = $parts[$parts][0];
$yearArray[] = $parts[$parts][1];
// ...
}
Then build a new structure with the new array parts:
foreach($dataArray as $key => $value){
$desiredArray[] = $spotArray[["user"[$yearArray[$hourArray]]], "first"[/** ... */]];
//...
}
Expected result:
$desiredArray = [
"AAAAA" => [
"user" => [
"ID:20fjrjeZZ" => ["01:04:00"],
"ID:13454B43A" => ["01:18:08", "04:10:12"],
"ID:18f5hjeWe" => ["02:00:02"]
],
"first" => "01:04:00",
"last" => "04:10:12",
"totaUser" => 3,
"totalAccess" => 4
],
"BBBBB" => [
"user" => [
"ID:Xj74320fj" => ["01:44:10"],
"ID:13454B43A" => ["07:08:58"],
"ID:Ftzkk800Y" => ["08:40:52"],
"ID:18f5hjeWe" => ["14:10:13"]
],
"first" => "01:44:10",
"last" => "14:10:13",
"totaUser" => 4,
"totalAccess" => 4
]
];
It is not at all necessary to run two loops.
Parse the space-delimited strings in your array and build/overwrite/sum as you iterate.
Code: (Demo)
$result = [];
foreach ($dataArray as $row) {
[$group, $y, $m, $d, $t, $id] = explode(' ', $row);
$result[$group]['user'][$id][] = $t; // accumulate nested elements
$result[$group]['first'] ??= $t; // only store the first occurrence
$result[$group]['last'] = $t; // keep overwriting each time
$result[$group]['totaluser'] = count($result[$group]['user']); // count what is accumulated
$result[$group]['totalAccess'] = ($result[$group]['totalAccess'] ?? 0) + 1; // increment
}
var_export($result);
You can even safely remove the unused $y, $m, and $d declarations if you wish. (Demo)
Output (from either snippet)
array (
'AAAAA' =>
array (
'user' =>
array (
'ID:20fjrjeZZ' =>
array (
0 => '01:04:00',
),
'ID:13454B43A' =>
array (
0 => '01:18:08',
1 => '04:10:13',
),
'ID:18f5hjeWe' =>
array (
0 => '02:00:02',
),
),
'first' => '01:04:00',
'last' => '04:10:13',
'totaluser' => 3,
'totalAccess' => 4,
),
'BBBBB' =>
array (
'user' =>
array (
'ID:Xj74320fj' =>
array (
0 => '01:44:10',
),
'ID:13454B43A' =>
array (
0 => '07:08:58',
),
'ID:Ftzkk800Y' =>
array (
0 => '08:40:52',
),
'ID:18f5hjeWe' =>
array (
0 => '14:10:13',
),
),
'first' => '01:44:10',
'last' => '14:10:13',
'totaluser' => 4,
'totalAccess' => 4,
),
)
You can find the answer to your question here
<?php
$dataArray = [
0 => "AAAAA 2023 01 25 01:04:00 ID:20fjrjeZZ",
1 => "AAAAA 2023 01 25 01:18:08 ID:13454B43A",
2 => "AAAAA 2023 01 25 02:00:02 ID:18f5hjeWe",
3 => "AAAAA 2023 01 25 04:10:13 ID:13454B43A",
4 => "BBBBB 2023 01 25 01:44:10 ID:Xj74320fj",
5 => "BBBBB 2023 01 25 07:08:58 ID:13454B43A",
6 => "BBBBB 2023 01 25 08:40:52 ID:Ftzkk800Y",
7 => "BBBBB 2023 01 25 14:10:13 ID:18f5hjeWe"
];
$finalArr = array();
$count_arr = array();
$count_arr1 = array();
foreach($dataArray as $parts){
$lines = explode(' ', $parts);
$finalArr[$lines[0]]['user'][$lines[5]][] = $lines[4];
$count_arr1[$lines[0]]['user'][$lines[5]] = $lines[4];
$count_arr[$lines[0]][] = 1;
}
foreach($finalArr as $key => $parts){
$finalArr[$key]['first'] = reset($count_arr1[$key]['user']);
$finalArr[$key]['last'] = end($count_arr1[$key]['user']);
$finalArr[$key]['totaUser'] = count($finalArr[$key]['user']);
$finalArr[$key]['totalAccess'] = count($count_arr[$key]);
}
print_r($finalArr);
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
Im trying to figure out how to sort the array below so that past array items are sent to the end of the array staying in start_date descending order.
Edit. Id probably include a time array key value item in all arrays to sort by start_date.
[216] => Array (
[title] => Production 1
[start_date] => 20th Feb
[end_date] => 23rd Feb 2015
[ticket_link] => http://www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/3514/1762/4350/Biz-Bio-Pic.jpg
[past] => 1
)
[218] => Array(
[title] => Production 3
[start_date] => 27th Feb
[end_date] => 2nd Mar 2015
[ticket_link] => www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/9414/1762/4351/Dan-Bio-Pic.jpg
[past] => 1
)
[219] => Array (
[title] => Production 4
[start_date] => 3rd Mar
[end_date] => 5th Mar 2015
[ticket_link] => www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/4314/1762/4351/Kate-Bio-Pic.jpg
[past] => 0
)
Try this -
function checkdate($a, $b)
{
$a = strtotime($a['start_date']);
$b = strtotime($b['start_date']);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
function checkpast($a, $b)
{
$a_start = strtotime($a['start_date']);
$b_start = strtotime($b['start_date']);
if ($a_start == b_start ) {
return ($a['past'] > $b['past']) ? -1 : 1;
}
}
$array = //your array
usort($array, "checkdate");
usort($array, "checkpast");
I have this array $theme_name
Array
(
[0] => template0
[1] => template1
[2] => template2
[3] => template3
[4] => template4
[5] => template5
[6] => template6
)
and this other array that has the same lenght $theme_info
Array
(
[0] => my template n 00
[1] => my template n 01
[2] => my template n 02
[3] => my template n 03
[4] => my template n 04
[5] => my template n 05
[6] => my template n 06
)
Basically what I want is to have this array :
Array
(
[template0] => my template n 00
[template1] => my template n 01
[template2] => my template n 02
[template3] => my template n 03
[template4] => my template n 04
[template5] => my template n 05
[template6] => my template n 06
)
Why this won't work ?
foreach ($themes_info as $key => $value) {
include($value['directory']) ;
$theme_info[] = $info;
$theme_name[] = $value['name'];
}
foreach ($theme_name as $key => $value) {
$value = $theme_info[$key];
}
FYI $themes_info have all the themes with names and directories emplacement.
Use array_combine():
$result = array_combine($theme_name, $theme_info);
Demo
You can do this far easier in PHP. Just like this:
for ($i = 0; $i < count($themes_info); $i++) {
include($value['directory']);
$theme_info[] = $info;
$theme_name["someKey$i"] = $value['name'];
}
And replace someKey with your prefered key for that array. Or for any other array for that matter.
I would like to find the date in a string after a particular word (key).
My string is dynamic and the date format also not same from one string to another string.
$data = "Balance- 0.30,Val-Aug 29 2013, Free Bal- 0.00";
or
$data = "Bal: 96.27.Valid Sep 26 2013.Toll Free Dial 578785";
or
$data = "BalanceRs.0.00,Expiry date: Apr 04 20141 Live Scores";
or
$data = "Your current balance is 0.20.Your account expires on 2013-11-23 23:59:59.";
or
$data = "Main Bal Rs.87.850 Val 09-07-2014,More";
$key = array('Val-','Val','Valid','Expiry date:','expires on');
$result=preg_match_all("/(?<=(".$key."))(\s\w*)/i",$data,$networkID);
$myanswer = #$networkID[0][0];
Here I am getting the output of only the first word.
Anyone please guide me to get the date. Thanks.
How about:
$data = "Balance- 0.30,Val-Aug 29 2013, Free Bal- 0.00";
$data .= "Bal: 96.27.Valid Sep 26 2013.Toll Free Dial 578785";
$data .= "BalanceRs.0.00,Expiry date: Apr 04 20141 Live Scores";
$data .= "Your current balance is 0.20.Your account expires on 2013-11-23 23:59:59.";
$data .= "Main Bal Rs.87.850 Val 09-07-2014,More";
$key = array('Valid','Val-','Val','Expiry date:','expires on');
$key_str = implode('|', $key);
preg_match_all("/(?<=$key_str)\s*((?:\w{3} \d\d \d{4})|(?:\d{4}-\d\d-\d\d)|(?:\d\d-\d\d-\d{4}))/i", $data, $networkID);
print_r($networkID);
output:
Array
(
[0] => Array
(
[0] => Aug 29 2013
[1] => Sep 26 2013
[2] => Apr 04 2014
[3] => 2013-11-23
[4] => 09-07-2014
)
[1] => Array
(
[0] => Aug 29 2013
[1] => Sep 26 2013
[2] => Apr 04 2014
[3] => 2013-11-23
[4] => 09-07-2014
)
)
All values of array $A are string the same length.
$A = Array
(
[0] => 03
[1] => 04
[2] => 05
[3] => 06
// [4] => 07 // "07" before "04" position
[4] => 04
[5] => 05
[6] => 06
// [8] => 07 // "07" before "08" position
[7] => 08
[8] => 03
[9] => 04
[10] => 05
[11] => 06
[12] => 07 // it is existing
[13] => 08
) ;
I want to Insert the "07" element if it is not existing before "04" or "08" position.start from position 1
So It will be after changed
$A = Array
(
[0] => 03
[1] => 04
[2] => 05
[3] => 06
[4] => 07 // just appended
[5] => 04
[6] => 05
[7] => 06
[8] => 07 // just append
[9] => 08
[10] => 03
[11] => 04
[12] => 05
[13] => 06
[14] => 07
[15] => 08
) ;
Anybody know how to do this ,help me please?
There would be "prettier" ways to do this but, as intended...
iterate the array
if the current value is equal to 7 minus 1 you will insert a new value there
create a function "insert_into_array" that:
a) Splits your array in two (look at array_chunk)
b) POPs your element to the end of the first array (array_pop)
c) merges your two arrays back (array_merge)
I've abstained from writing any code as this is probably homework and, writing code, even if you're not really deep thinking the problem will push you a long way to passing the exam...
not the most beautiful solution, but should do the job:
$b = array();
for($i=0;$i<count($A);$i++){
$b[] = $A[$i];
if(($i<count($A) - 1) && ($A[$i+1]<$A[$i] || ($A[$i+1] == '08')) && $A[$i] < '07')
$b[] = '07';
}
var_dump($b);
First, find the gaps in your array, that is the positions where there's 06 but not a following 07:
$positions = array();
foreach ($A as $k => $v) {
if (isset($last) && $last != $v - 1 && $last == '06') {
$positions[] = $k;
}
$last = $v;
}
Then, insert them:
$count = 0;
foreach ($positions as $pos) {
array_splice($A, $pos + ($count++), 0, '07');
}
That's it.
//make sure the array is numeric:
$A = array_values($A);
foreach(array('04','08') as $search){
$positions = array_keys($A,$search);
rsort($positions);
foreach($positions as $key){
if($key==0 || $A[$key-1] != '07'){
array_splice($A,$key,0,'O7');
}
}
}
In 2017, I've found 2 beautiful methods that is part of nette\utils package.
Arrays::insertBefore()
Arrays::insertAfter()
They do job perfectly!
Just run:
composer require nette/utils
and use Arrays class or inspire in their code.