I generate this array $months from an awstats file that looks like this :
Array
(
[0] => Array
(
[0] => 10
[1] => 37
[2] => 11
)
[1] => Array
(
[0] => 11
[1] => 34
[2] => 19
)
[2] => Array
(
[0] => 12
[1] => 20
[2] => 11
)
)
Which $months[0][0] represent the number of a month and $months[0][1] it's data. What I'd like to do is to build an array that has all the number of months of the year with it's data and if it doesn't have one make it to 0.
Like this :
Array
(
[0] => Array
(
[0] => 01
[1] => 41
[2] => 52
)
[1] => Array
(
[0] => 02
[1] => 74
[2] => 66
)
[2] => Array
(
[0] => 03
[1] => 40
[2] => 83
)
[3] => Array
(
[0] => 04
[1] => 0
[2] => 0
)
[4] => Array
(
[0] => 05
[1] => 0
[2] => 0
)
[5] => Array
(
[0] => 06
[1] => 0
[2] => 0
)
and so on... Is there any way I can achieve this?Much appreciated!
Here's my idea (if I understood your question right):
First, create another array ($sortedMonths) with 12 elements and fill them with 'empty data':
$sortedMonths = array();
for ($i = 1; $i <= 12; $i++) {
$sortedMonths[] = array($i, 0, 0);
}
/*
Array
(
[0] => Array
(
[0] => 1,
[1] => 0,
[2] => 0
)
...
[11] => Array
(
[0] => 12,
[1] => 0,
[2] => 0
)
)
*/
Then loop through $months and assign data to the appropriate element of $sortedMonths:
foreach ($months as $month) {
$sortedMonths[$month[0]-1] = $month;
}
Related
I new to PHP, I need to convert single dimension array into multi dimension array in php
I have data like this, need to minimize as below.
Array
(
[0] => Array
(
[0] => David
[1] => School
[2] => 19
[3] => 29
)
[1] => Array
(
[0] => Paul
[1] => Home
[2] => 19
[3] => 29
)
[2] => Array
(
[0] => Paul
[1] => Cinema
[2] => 19
[3] => 29
)
[3] => Array
(
[0] => Paul
[1] => Park
[2] => 19
[3] => 29
)
[4] => Array
(
[0] => Rossie
[1] => Playground
[2] => 19
[3] => 29
)
[5] => Array
(
[0] => Rossie
[1] => Hotel
[2] => 19
[3] => 29
)
[6] => Array
(
[0] => Rossie
[1] => Hospital
[2] => 19
[3] => 29
)
)
And I want convert it to multidimensional
Array
(
[0] => Array
(
[0] => Array
(
[0] => David
(
[0] => School
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Paul
(
[0] => Home
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Cinema
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Park
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[2] => Array
(
[0] => Array
(
[0] => Rossie
(
[0] => Playground
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hotel
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hospital
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
)
I hope you get an idea. But my function doesn't do this correctly or maybe there are other ways to do this easier ?
I would be grateful for any help.
Thanks
Below is one way to do it:
<?php
$arr = [array('David','School',19,29),
array('Paul','Home',19,29),
array('Paul','Cinema',19,29),
array('Paul','Park',19,29),
array('Rossie','Playground',19,29),
array('Rossie','Hotel',19,29),
array('Rossie','Hospital',19,29)];
// Get all names.
$names = array_unique(array_map(function($value){return $value[0];}, $arr));
$places = [];
// Create the multidimensional array, grouping by name.
foreach($names as $key => $name){
$tempArr = [];
foreach($arr as $record){
if($record[0] === $name){
$tempArr[][$record[1]] = array_slice($record,2);
}
}
$places[][][$name] = $tempArr;
}
print("<pre>".print_r($places,true)."</pre>");
This will return the following result:
Array
(
[0] => Array
(
[0] => Array
(
[David] => Array
(
[0] => Array
(
[School] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[1] => Array
(
[0] => Array
(
[Paul] => Array
(
[0] => Array
(
[Home] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Cinema] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Park] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[2] => Array
(
[0] => Array
(
[Rossie] => Array
(
[0] => Array
(
[Playground] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Hotel] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Hospital] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
)
Below is the array in php and I need function to solve n numbers of array as displayed in Expected result by summing the (2) and (3) values if (0) and (1) are matching :
Array
(
[0] => Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 1
[3] => 2
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 3
[3] => 4
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 5
[3] => 6
)
)
[1] => Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 7
[3] => 8
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 9
[3] => 10
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 11
[3] => 12
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
)
Expected Result :
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
Need help to get the above expected result for the raised query. I tried below code :
<?php
$sum = [];
foreach($weekly as $aSet) {
foreach($aSet as $k => $kv) {
$i = 0;
foreach($kv as $v) {
$sum[$k][$i] += $v;
$i++;
}
}
}
?>
But it will add all types :
[0] => Array
(
[0] => 0
[1] => 4038
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => 0
[1] => 4038
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => 0
[1] => 4038
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => 0
[1] => 4038
[2] => 13
[3] => 14
)
I want get sum only for 2 and 3rd value and need to keep constant for 0 and 1st value for exact match
Because all your nested arrays are with predefined amount of elements, all of those with the same indexes, you can use a for loop instead of the third foreach:
$sum = [];
foreach($weekly as $aSet)
{
foreach($aSet as $k => $kv)
{
$sum[$kv[0]][0] = $kv[0];
$sum[$kv[0]][1] = $kv[1];
for($i = 2; $i <= 3; $i++)
{
$sum[$kv[0]][$i] += $kv[$i];
}
}
}
echo "<pre>";
print_r($sum);
echo "</pre>";
With a for loop you can cycle inside the array considering only the needed elements of that array (in your case the indexes 0 and 1 aren't useful in this loop).
Output:
Array
(
[Week - 1] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[Week - 2] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[Week - 3] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[Week - 4] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
If you prefer the array with numbered indexes do that:
echo "<pre>";
print_r(array_values($sum));
echo "</pre>";
Output:
Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
I have two array with same keys this two array contain month wise data of my table. i want sum of this values and return same keys sum of values in one other array
Here is my two array
array1
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 22 )
[2] => Array ( [0] => Mar [1] => 0 )
[3] => Array ( [0] => Apr [1] => 9 )
[4] => Array ( [0] => May [1] => 1 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
array 2:
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 0 )
[2] => Array ( [0] => Mar [1] => 18 )
[3] => Array ( [0] => Apr [1] => 1 )
[4] => Array ( [0] => May [1] => 1 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
i also tried using this code
function sum_arrays($array1, $array2) {
$array = array();
foreach($array1 as $index => $value) {
$array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
}
return $array;
}
i want result like below
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 22 )
[2] => Array ( [0] => Mar [1] => 18 )
[3] => Array ( [0] => Apr [1] => 10 )
[4] => Array ( [0] => May [1] => 2 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
array_map approach (if 2 arrays have month items in same order):
// shortened array samples
$arr1 = [
["Jan", 0],
["Feb", 22],
["Mar", 0]
];
$arr2 = [
["Jan", 0],
["Feb", 0],
["Mar", 18]
];
$result = array_map(function($a, $b){
return [$a[0], $a[1] + $b[1]];
}, $arr1, $arr2);
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Jan
[1] => 0
)
[1] => Array
(
[0] => Feb
[1] => 22
)
[2] => Array
(
[0] => Mar
[1] => 18
)
)
Using The foreach Loop:
$sum = 0;
foreach($items as $item) {
$sum += $item['qty'];
}
echo $sum;
Using array_map():
echo array_sum(array_map(
function($item) {
return $item['qty'];
}, $items)
);
Using array_reduce():
echo array_reduce($items, function($carry, $item) {
$carry += $item['qty'];
return $carry;
});
I have a series of variables that are collected in a while next loop.
I wanted to store these in an array so that I can access them later in some javascript, so I set
$remainingTime = array();
before while (!$listing->EOF) {
After the variables are populated I then added
$remainingTime[] = array( $remainingDay, $remainingHour, $remainingMinutes, $remainingSeconds );
If I do
echo "<pre>";
print_r($remainingTime);
echo "</pre>";
after the loop is finished I get the following output
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
[2] => Array
(
[0] => 8
[1] => 16
[2] => 4
[3] => 33
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
[2] => Array
(
[0] => 8
[1] => 16
[2] => 4
[3] => 33
)
[3] => Array
(
[0] => 2
[1] => 5
[2] => 38
[3] => 17
)
)
So it appears that it is building a new array each time it goes through the loop rather than just adding the next dataset to it.
Where have I gone wrong with building the array?
The assignment syntax you used ($remainingTime[]) pushes the value you assign it as a new element into the array. Try it without using the square brackets.
I'm getting values for my flot chart via ajax.
At the backend script my test array looks like this:
Array
(
[0] => Array
(
[0] => 6
[1] => 1
)
[1] => Array
(
[0] => 7
[1] => 7
)
[2] => Array
(
[0] => 8
[1] => 37
)
[3] => Array
(
[0] => 9
[1] => 44
)
)
The value with the offset [0] represents the hour.
Now I need 24 array objects for every hour. How to maintain this without touching the given elements?
e.g.
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[6] => Array
(
[0] => 6
[1] => 1
)
[...]
[7] => Array
(
[0] => 7
[1] => 7
)
[8] => Array
(
[0] => 8
[1] => 37
)
[9] => Array
(
[0] => 9
[1] => 44
)
Thanks in advance.
You want to "re-key" this result set according to the first subarray value, and then merge it into an array of 24 hour elements.
Try this:
$data = [
[6, 1],
[7, 7],
[8, 37],
[9, 44]
];
$hours = array_fill(0, 25, [0, 0]);
$data = array_combine(array_column($data, 0), $data);
$hours = array_replace($hours, $data);
print_r($hours);
This:
Creates a 24 element array $hours containing sub-arrays containing default values [0, 0]
Re-keys your $data, pulling out and using the first value of each sub-array to do so. This assumes you are using PHP 5.5.
Finally, replace the modified $data into $hours
This yields:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
)
[2] => Array
(
[0] => 0
[1] => 0
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 0
[1] => 0
)
[5] => Array
(
[0] => 0
[1] => 0
)
[6] => Array
(
[0] => 6
[1] => 1
)
[7] => Array
(
[0] => 7
[1] => 7
)
[8] => Array
(
[0] => 8
[1] => 37
)
[9] => Array
(
[0] => 9
[1] => 44
)
[10] => Array
(
[0] => 0
[1] => 0
)
[11] => Array
(
[0] => 0
[1] => 0
)
[12] => Array
(
[0] => 0
[1] => 0
)
[13] => Array
(
[0] => 0
[1] => 0
)
[14] => Array
(
[0] => 0
[1] => 0
)
[15] => Array
(
[0] => 0
[1] => 0
)
[16] => Array
(
[0] => 0
[1] => 0
)
[17] => Array
(
[0] => 0
[1] => 0
)
[18] => Array
(
[0] => 0
[1] => 0
)
[19] => Array
(
[0] => 0
[1] => 0
)
[20] => Array
(
[0] => 0
[1] => 0
)
[21] => Array
(
[0] => 0
[1] => 0
)
[22] => Array
(
[0] => 0
[1] => 0
)
[23] => Array
(
[0] => 0
[1] => 0
)
)
Hope this helps :)
You can try also this:
// yours test array
$times = array(
array(6,1),
array(7,7),
array(8,37),
array(9,44)
);
// array with all hours
$fullTimes = array_fill(0, 24, array(0,0));
foreach ($times as $time) {
$fullTimes[$time[0]] = $time;
}
Solution for me:
while ($stmt->fetch()) {
$x = $hour;
$y = $zugriffe;
$data1[$x] = array ($x, $y);
}
for ($i=0;$i<=24;$i++){
if (!isset($data1[$i])){
$data1[$i] = array ($i,0);
}
}
sort($data1);
Assume the array's name is $a, you can do like this:
for($i = 0; $i < 24; $i++) {
if(isset($a[$i])) continue;
else {
$a[$i] = array($i,"something you like here");
}
}