Sum and Average in multi-dimentional arrays with php - php

I'm trying to get sum and average of visitors from the following multi-dimensional array :
Array([visitors] => Array(
[2015-06-12] => Array([0] => Array([value] => 29))
[2015-06-11] => Array([0] => Array([value] => 55))
...
))
I cannot manage to find a way to get the results i need as i get lost with "foreach".
Can anybody help please ?

Use this
<?php
$mainarray = array('visitors' => Array(
'2015-06-12' => Array(Array('value' => 29)),
'2015-06-11' => Array(Array('value' => 55))));
$sum = 0;
$count = 0;
$visitor = $mainarray['visitors'];
foreach ($visitor as $key => $val) {
$sum += $val[0]['value'];
$count++;
}
echo "Sum is " . $sum."<br>";
$average = ($sum / $count);
echo "Average is " .$average."<br>";;
?>

Related

Calculating with multi arrays

I need help with some of my code. I need to use the numbers in prijs for a calculation.
$autos = array(
"<b>Mercedes</b>" =>array(
"Kenteken" => "77NLXJ",
"Prijs" => "54800",
),
"<b>Tesla</b>" =>array(
"Kenteken" => "GV713G",
"Prijs" => "70700",
),
"<b>Porsche</b>" =>array(
"Kenteken" => "GG101K",
"Prijs" => "85000",
)
Is this possible?
$keys = array_keys($autos);
for($i = 0; $i < count($autos); $i++) {
echo $keys[$i] . "<br>";
foreach($autos[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
}
echo "<br>";
}
To access and change them, you need to access them through the $autos array:
//by loop:
foreach($autos as $key => $auto){
//get or set them with this var
$autos[$key]["Prijs"];
}
//to get and set them directly:
$autos[0]["Prijs"];
$autos[1]["Prijs"];
$autos[2]["Prijs"];

php associative array select 1 random value with key

I need to random a single element from the array. I have code ;
if (isset($_POST['losuj'])) {
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/2.jpg',
'mienso3' => 'skiny/2.jpg',
'mienso4' => 'skiny/2.jpg',
'mienso5' => 'skiny/2.jpg',
'Hasasdasd' => 'skiny/2.jpg',
];
foreach($arr as $key => $value) {
$keys = array_rand( $arr, 1);
echo $keys;
}
}
And its didnt working. Any tips ?
You can use array_keys to get the keys in a indexed array.
The just use array_rand just like you did to pick one and echo the $arr associative key.
$keys = array_keys($arr);
$random = $keys[array_rand($keys,1)];
Echo $random . " => " . $arr[$random];
https://3v4l.org/miacb
Just use array_rand($arr,1) without foreach loop
With PHP we can use the function array_rand()
More information can be found at:
http://php.net/manual/en/function.array-rand.php
https://www.w3schools.com/php/func_array_rand.asp
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/2.jpg',
'mienso3' => 'skiny/2.jpg',
'mienso4' => 'skiny/2.jpg',
'mienso5' => 'skiny/2.jpg',
'Hasasdasd' => 'skiny/2.jpg',
];
$randomEntry = array_rand($arr, 1);
use this without the loop
$key = array_rand($arr);
echo $arr[$key];
full example
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/3.jpg',
'mienso3' => 'skiny/4.jpg',
'mienso4' => 'skiny/5.jpg',
'mienso5' => 'skiny/6.jpg',
'Hasasdasd' => 'skiny/7.jpg',
];
$key = array_rand($arr);
echo $key;
echo $arr[$key];
You should print array result like below,
$rand_keys = array_rand($arr, 1);
echo $arr[$rand_keys[0]] . "\n";
Are the '.jpg' files meant to all be the same except for one? Because randomly choosing between 7 files when 6 are the same is going to return the same file more than often.
$rand_keys = array_rand($arr);
echo $arr[$rand_keys];
<?php
$input = array(
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/3.jpg',
'mienso3' => 'skiny/4.jpg',
'mienso4' => 'skiny/5.jpg',
'mienso5' => 'skiny/6.jpg',
'Hasasdasd' => 'skiny/7.jpg',
);
foreach($input as $key => $value) {
$keys = array_rand( $input, 1);
echo $input[$keys];
}
?>

Sum values in array and simplify key names

This is almost similar to my other question which is related to the same project I'm working on.. Link to my other question
but in this case the array is different as follow:
Array
(
[2014-08-01 11:27:03] => 2
[2014-08-01 11:52:57] => 2
[2014-08-01 11:54:49] => 2
[2014-08-02 11:59:54] => 4
[2014-08-02 12:02:41] => 2
[2014-08-05 12:09:38] => 4
[2014-08-07 12:23:12] => 3
[2014-08-07 12:25:18] => 3
// and so on...
)
That is my output array and in order to get that array I had to do some miracles... anyway, so based on that array I have to sum the value for each key date and build an array something like this...
Array
(
[2014-08-01] => 6
[2014-08-02] => 6
[2014-08-05] => 4
[2014-08-07] => 6
// and so on...
)
That last array will be use to build graphs with morrisonJS, what I have is this:
$res_meno = array();
foreach ($sunArr as $keys => $values) {
$arrays= explode(" ",$sumArr[$keys]);
$res_meno[] = $arrays[0];
}
$vals_char2 = array_count_values($res_meno);
That is my attempt to build my last array but is not working...
any help would be greatly appreciated!
Thank you for taking the time.
Try this code
<?php
$arr = array(
"2014-08-01 11:27:03" => 2,
"2014-08-01 11:52:57" => 2,
"2014-08-01 11:54:49" => 2,
"2014-08-02 11:59:54" => 4,
"2014-08-02 12:02:41" => 2,
"2014-08-05 12:09:38" => 4,
"2014-08-07 12:23:12" => 3,
"2014-08-07 12:25:18" => 3
);
$new_array = array();
foreach($arr as $k => $v){
$date = reset(explode(" ", $k));
if(isset($new_array[$date])){
$new_array[$date] += $v;
}
else{
$new_array[$date] = $v;
}
}
print_r($new_array);
?>
DEMO
$sunArr = array
(
"2014-08-01 11:27:03" => 2,
"2014-08-01 11:52:57" => 2,
"2014-08-01 11:54:49" => 2,
"2014-08-02 11:59:54" => 4,
"2014-08-02 12:02:41" => 2,
"2014-08-05 12:09:38" => 4,
"2014-08-07 12:23:12" => 3,
"2014-08-07 12:25:18" => 3,
);
$res_meno = array();
foreach ($sunArr as $keys => $values) {
$arrays= explode(" ",$keys);
if(isset($res_meno[$arrays[0]]))
{
$res_meno[$arrays[0]] = $res_meno[$arrays[0]] + $values;
}
else
{
$res_meno[$arrays[0]] = $values;
}
}
print_r($res_meno);
exit;
Try this, i think it might fix the problem
Try this PHP Code You Can test here
$sunArr = Array
(
'2014-08-01 11:27:03' => 2,
'2014-08-01 11:52:57' => 2,
'2014-08-01 11:54:49' => 2,
'2014-08-02 11:59:54' => 4,
'2014-08-02 12:02:41' => 2,
'2014-08-05 12:09:3' => 4,
'2014-08-07 12:23:12' => 3,
'2014-08-07 12:25:18' => 3
);
$key = 0;
$res_meno = array();
foreach ($sunArr as $keys => $values)
{
$ar= explode(" ", $keys);
if( $key == $ar[0] )
{
$res_meno[$key] = $sunArr[$keys] + $res_meno[$key];
}
else
{
$key = $ar[0];
$res_meno[$key] = $values;
}
}
echo '<pre>';
print_r($res_meno);
die;
Where does the first array come from? If it's from a SQL database, it's better to create a query that returns the aggregated array.
Otherwise no need to use array_key_values for that:
$res_meno = array();
foreach ($sumArr as $keys => $values) {
$key = substr($keys, 0, 10);
$res_meno[$key] = (empty($res_meno[$key]) ? 0 : $res_meno[$key]) + $values;
}
Here is a solution which uses a callback. Not to use loops is often better!
$sunArr = array(
'2014-08-01 11:27:03' => 3,
'2014-08-01 11:27:05' => 5,
'2013-09-01 11:01:05' => 1
);
$res = array();
function map($item, $key, &$result)
{
$result[current(explode(" ", $key))] += $item;
}
array_walk($sunArr, "map", &$res);
var_dump($res);
You can test it here on codepad.

Compare $Variables Results, and then orden in different Ways

Hello i just finish a code where i get like 50 variables... all of them with int values..
I have the variables as separete values, just for this example I will set the variables with the result, BUT the result came from other evaluations and stuff that its fine, cause im already echoing a verified result.
$one = 13
$two = 35
$three = 46
The "item1" appears <?PHP echo $one; ?> times<br />
The "item2" appears <?PHP echo $two; ?> times<br />
The "item3" appears <?PHP echo $three; ?> times<br />
This is fine but,, How can i order the results, in ASC way or DSC , to build a order by...
Thanks so much
This far this is working great
$naturales = array(
$uno => "n1",
$dos => "n2",
$tres => "n3",
$cuatro => "n4",
$cinco => "n5",
$seis => "n6",
$siete => "n7",
$ocho => "n8",
$nueve => "n9",
$diez => "n10",
$once => "n11",
$doce => "n12",
$trece => "n13",
$catorce => "n14",
$quince => "n15",
$dieciseis => "n16",
$diecisiete => "n17",
$dieciocho => "n18",
$diecinueve => "n19",
$veinte => "n20",
$veintiuno => "n21",
$veintidos => "n22",
$veintitres => "n23",
$veinticuatro => "n24",
$veinticinco => "n25",
$veintiseis => "n26",
$veintisiete => "n27",
$veintiocho => "n28",
$veintinueve => "n29",
$treinta => "n30",
$treintayuno => "n31",
$treintaydos => "n32",
$treintaytres => "n33",
$treintaycuatro => "n34",
$treintaycinco => "n35",
$treintayseis => "n36",
$treintaysiete => "n37",
$treintayocho => "n38",
$treintaynueve => "n39",
$cuarenta => "n40",
$cuarentayuno => "n41",
$cuarentaydos => "n42",
$cuarentaytres => "n43",
$cuarentaycuatro => "n44",
$cuarentaycinco => "n45",
$cuarentayseis => "n46",
$cuarentaysiete => "n47",
$cuarentayocho => "n48",
$cuarentaynueve => "n49",
$cincuenta => "n50",
$cincuentayuno => "n51",
$cincuentaydos => "n52",
$cincuentaytres => "n53",
$cincuentaycuatro => "n54",
$cincuentaycinco => "n55",
$cincuentayseis => "n56",
);
krsort($naturales);
foreach ($naturales as $count => $name) {
echo "The \"$name\" appears $count times<br />";
}
Why my results are like this (Its hidding all the results with 12 (Similar count results)
for example for "n3" appears 12 times. and its not listed.
The "n20" appears 12 times
The "n30" appears 11 times
The "n37" appears 10 times
The "n41" appears 9 times
The "n42" appears 8 times
The "n45" appears 7 times
The "n47" appears 6 times
The "n35" appears 5 times
The "n44" appears 4 times
The "n46" appears 2 times
The "n56" appears 0 times
Build an array
$myresults = array("Item1"=>13,"item2"=>35,"item3"=>46);
then use asort() or arsort() on the array $myresults
then do a for/foreach loop to output the results
basic guidelines but off this you should be able to google how to implement in detail fairly easily (even on here will work)
$one = 13;
$two = 35;
$three = 46;
$arr = array("Item 1"=>$one,"Item 2"=>$two,"Item 3"=>$three);
echo "<strong>Original</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
asort($arr);
echo "<strong>Ascending Sort</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
arsort($arr);
echo "<strong>Descending Sort</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
As previously mentioned, you can use asort and arsort to sort your array as needed... I'm adding some examples here as well as some working CODE
As mentioned, you could insert your values into an associative array, i.e.:
$items = array(
$one => "item1",
$two => "item2",
$three => "item3"
);
and then you can use a function like ksort() to sort all of your values:
http://php.net/manual/en/function.ksort.php
so you can end up with something like this:
ksort($items);
foreach ($items as $count => $name) {
echo "The \"$name\" appears $count times<br />";
}

Find Longest Span of Consecutive Array Keys

I've got an array that uses UNIX timestamps for array keys. The array will typically hold data for anywhere from 15 minutes to maybe an hours worth of time, however there are only entries for seconds that have data.
Most of the data will be spread out, there will be occasional spans of data for consecutive seconds though. What I'd like to do, is retrieve the first and last second of the longest consecutive span of seconds in the array.
If I have this array
Array
(
[1276033307] => 119.0
[1276033331] => 281.8
[1276033425] => 28.2
[1276033431] => 88.2
[1276033432] => 196.2
[1276034207] => 205.5
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
[1276034230] => 79.8
)
I would either need the keys 1276034226 and 1276034228, or the following array returned.
Array
(
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
)
EDIT:
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$finalArray = array();
foreach($array as $k => $v){
$tempArrays = array();
$index = 0;
while(isset($array[$k + $index])){
$tempArrays[$k+$index] = $array[$k+$index++];
}
if(count($tempArrays) > count($finalArray))
$finalArray = $tempArrays;
}
print_r($finalArray);
the output is the same...
ORIGINAL:
Note: Only the first occurrence of the longest span will be recorded.
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$longspan = 0;
foreach($array as $k => $v){
$index = 1;
while(isset($array[$k+$index])){
$index++;
}
$curspan = --$index;
if($curspan > $longspan){
$longspan = $curspan;
$start = $k;
}
}
for($i=0; $i <= $longspan; $i++)
$results[$start + $i] = $array[$start + $i];
print_r($results);
Outputs:
Array (
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
)
This code does it in a single loop (i.e. is a Greedy algorithm):
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$long_arr = array();
$curr_arr = array();
$last_key = -1;
foreach($array as $k => $v) {
if ($k != $last_key + 1) {
$curr_arr = array();
}
$curr_arr[$k] = $v;
if (count($curr_arr) > count($long_arr)) {
$long_arr = $curr_arr;
}
$last_key = $k;
}
print_r($long_arr);

Categories