Filling an array with missing values afterwards - php

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");
}
}

Related

Display a sum of two array values with same keys

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;
});

Compare values of two multidimentional array and insert if not exits

I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){

i want to show set to array which is not empty, if search hits empty array then count upto 10 still empty array then close array and continue

in the following array i want to show from array[0] to array[2] is pack1, array[5] to array[10] pack2, array[10] to array[12] is pack3 pack4 starts from array[14]. (if empty array is above 2 then close the pack and start another pack)
array
(
[0] => array
(
[id] => 1
[num] => 980909
)
[1] => array
(
[id] => 2
[num] => 090909
)
[2] => array
(
[id] => 3
[num] => 909
)
[3] => array
(
)
[4] => array
(
)
[5] => array
(
[id] => 6
[num] => 6565
)
[6] => array
(
[id] => 7
[num] => 6565
)
[7] => array
(
[id] => 8
[num] => 65
)
[8] => array
(
)
[9] => array
(
[id] => 10
[num] => 665
)
[10] => array
(
[id] => 11
[num] => 600
)
[11] => array
(
)
[12] => array
(
)
[13] => array
(
)
[14] => array
(
[id] => 15
[num] => 700
)
$datas = array();
$empcount = 10; $emp = $dIndex = 0;
for($i=0;$i
if( $emp >= $empcount) { $dIndex++; $emp = 0; } $datas[$dIndex][] = $finaldata[$i]; }

building a multidimention array php

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;
}

Split an array into mutiple arrays based on a value - PHP

What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.

Categories