Its been four hours I have search everywhere on google and SOF but unable to find the answer. This is what i have tried so far. Here is my code so far
$tmp_array = array();
foreach ($cms as $key => $val) {
$cDate = date('Ym', strtotime($val['day_date']));
$tmp_ids[] = $val['id'];
if (array_key_exists($cDate, $tmp_array)) {
$tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
$tmp_array[$cDate]['ids'] = $tmp_ids;
} else {
$tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
$tmp_array[$cDate]['ids'] = $tmp_ids;
}
}
its output is coming like this,
Array
(
[202001] => Array
(
[new_visitors] => 797
[ids] => Array
(
[0] => 31
[1] => 32
)
)
[202002] => Array
(
[new_visitors] => 461
[ids] => Array
(
[0] => 31
[1] => 32
[2] => 33
[3] => 34
)
)
)
but i want the result array like this,
Array
(
[202001] => Array
(
[new_visitors] => 797
[ids] => Array
(
[0] => 31
[1] => 32
)
)
[202002] => Array
(
[new_visitors] => 461
[ids] => Array
(
[0] => 33
[1] => 34
)
)
)
Any suggestions, what am i doing in my code?
The ids are basicall primary key of the table and the "new_visitors" are count of the visitor those who visit on my site. but i havent find any solution so far.
Here is my $cms array.
Array
(
[0] => Array
(
[id] => 31
[day_date] => 2020-01-30 00:00:00
[new_visitors] => 459
)
[1] => Array
(
[id] => 32
[day_date] => 2020-01-31 00:00:00
[new_visitors] => 338
)
[2] => Array
(
[id] => 33
[day_date] => 2020-02-01 00:00:00
[new_visitors] => 242
)
[3] => Array
(
[id] => 34
[day_date] => 2020-02-02 00:00:00
[new_visitors] => 219
)
)
Thank you misorude, your comment worked
$tmp_array = array();
foreach ($cms as $key => $val) {
$cDate = date('Ym', strtotime($val['day_date']));
if (array_key_exists($cDate, $tmp_array)) {
$tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
$tmp_array[$cDate]['ids'][] = $val['id'];
} else {
$tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
$tmp_array[$cDate]['ids'][] = $val['id'];
}
}
$arr = [
['id' => 31 ,'day_date'=> "2020-01-30 00:00:00", 'new_visitors' => 459],
['id' => 32 ,'day_date'=> "2020-01-31 00:00:00", 'new_visitors' => 338],
['id' => 33 ,'day_date'=> "2020-02-01 00:00:00", 'new_visitors' => 242],
['id' => 34 ,'day_date'=> "2020-02-02 00:00:00", 'new_visitors' => 219]
];
echo "<pre>";
print_r($arr);
$data = [];
$add_visitor = 0;
$day_date = '';
foreach($arr as $arr_value){
if(date('Ym', strtotime($arr_value['day_date'])) != $day_date){
$add_visitor = 0;
}
$add_visitor += $arr_value['new_visitors'];
$data[date('Ym', strtotime($arr_value['day_date']))]['new_visitors'] = $add_visitor;
$data[date('Ym', strtotime($arr_value['day_date']))]['ids'][] = $arr_value['id'];
$day_date = date('Ym', strtotime($arr_value['day_date']));
}
print_r($data);
Related
We have an array in PHP like below:
Array
(
[0] => Array
(
[id] => 29
[name] => Testing1
)
[1] => Array
(
[id] => 30
[name] => Testing2
)
[2] => Array
(
[id] => 31
[name] => Testing3
)
)
We also have a variable $getvalue . We want to create a function (we will use the function in for loop) in which we pass above array and $getvalue. If $getvalue = 2 it should return key[0] 2 time, key[1] 2 time and key[2] 2 time like below.
[0] => Array
(
[id] => 29
[name] => Testing1
)
[0] => Array
(
[id] => 29
[name] => Testing1
)
[1] => Array
(
[id] => 30
[name] => Testing2
)
[1] => Array
(
[id] => 30
[name] => Testing2
)
[2] => Array
(
[id] => 31
[name] => Testing3
)
[2] => Array
(
[id] => 31
[name] => Testing3
)
If $getvalue = 1 it should return key[0] 1 time, key[1] 1 time and key[2] 1 time like below.
[0] => Array
(
[id] => 29
[name] => Testing1
)
[1] => Array
(
[id] => 30
[name] => Testing2
)
[2] => Array
(
[id] => 31
[name] => Testing3
)
Tried :
for($i = 0; $i<=count($array); $i++) {
foreach($array as $key => $val) {
if($i==$getvalue )
{
$a[] = $array[$i+1];
}
else
{
$a[] = $array[$i];
}
}
}
Also tried :
static function abc ($array,$getvalue)
{
foreach ($array as $key => $value) {
for ($i=0; $i <= $getvalue; $i++) {
return $arr[$i];
}
}
}
Each element of input array is added to the return value $getvalue times:
<?php
function repeatify( array $array, int $getvalue ): array {
$out = [];
if($getvalue <= 0) return $out;
foreach( $array as $element ){
foreach( range(0, $getvalue - 1) as $we_actualy_are_not_using_this_var )
$out[] = $element;
# Or alternatively with a `for` loop (i'll comment this out since i prefer an above code):
/*
for (
$we_actualy_are_not_using_this_var = 0;
$we_actualy_are_not_using_this_var < $getvalue;
$we_actualy_are_not_using_this_var++
) {
$out[] = $element;
}
*/
}
return $out;
}
$data = [
[
'id' => 29,
'name' => 'Testing1',
],
[
'id' => 30,
'name' => 'Testing2',
],
[
'id' => 31,
'name' => 'Testing2',
],
];
print '<pre>';
print_r( repeatify( $data, 0 ) );
print_r( repeatify( $data, 1 ) );
print_r( repeatify( $data, 2 ) );
how to display this using foreach in php?
I want to show it as a list.
$quantity=0;
$quantities = array(
array('Jan',($quantity += $ppmpitem->m1)),
array('Feb',($quantity += $ppmpitem->m2)),
array('Mar',($quantity += $ppmpitem->m3)),
array('Apr',($quantity += $ppmpitem->m4)),
array('May',($quantity += $ppmpitem->m5)),
array('Jun',($quantity += $ppmpitem->m6)),
array('Jul',($quantity += $ppmpitem->m7)),
array('Aug',($quantity += $ppmpitem->m8)),
array('Sep',($quantity += $ppmpitem->m9)),
array('Oct',($quantity += $ppmpitem->m10)),
array('Nov',($quantity += $ppmpitem->m11)),
array('Dec',($quantity += $ppmpitem->m12)),
);
return $quantities;
You have several options, so based on your code (and what I added):
<?php
$quantity=0;
// Added this, so we have an object
$array = [
"m1" => 1,
"m2" => 2,
"m3" => 3,
"m4" => 4,
"m5" => 5,
"m6" => 6,
"m7" => 7,
"m8" => 8,
"m9" => 9,
"m10" => 10,
"m11" => 11,
"m12" => 12
];
// Converted array to object
$ppmpitem = json_decode(json_encode($array));
$quantities = array(
array('Jan',($quantity += $ppmpitem->m1)),
array('Feb',($quantity += $ppmpitem->m2)),
array('Mar',($quantity += $ppmpitem->m3)),
array('Apr',($quantity += $ppmpitem->m4)),
array('May',($quantity += $ppmpitem->m5)),
array('Jun',($quantity += $ppmpitem->m6)),
array('Jul',($quantity += $ppmpitem->m7)),
array('Aug',($quantity += $ppmpitem->m8)),
array('Sep',($quantity += $ppmpitem->m9)),
array('Oct',($quantity += $ppmpitem->m10)),
array('Nov',($quantity += $ppmpitem->m11)),
array('Dec',($quantity += $ppmpitem->m12)),
);
// Pretty print
print("<pre>".print_r($quantities,true)."</pre>");
// As a list where Month is key
foreach($quantities as $key => $value) {
$month = [
$quantities[$key][0] => $quantities[$key][1]
];
$months[$quantities[$key][0]] = $quantities[$key][1];
print_r($month);
}
//Also print full array of months:
print_r($months);
Results are:
Pretty print:
<pre>Array
(
[0] => Array
(
[0] => Jan
[1] => 1
)
[1] => Array
(
[0] => Feb
[1] => 3
)
[2] => Array
(
[0] => Mar
[1] => 6
)
[3] => Array
(
[0] => Apr
[1] => 10
)
[4] => Array
(
[0] => May
[1] => 15
)
[5] => Array
(
[0] => Jun
[1] => 21
)
[6] => Array
(
[0] => Jul
[1] => 28
)
[7] => Array
(
[0] => Aug
[1] => 36
)
[8] => Array
(
[0] => Sep
[1] => 45
)
[9] => Array
(
[0] => Oct
[1] => 55
)
[10] => Array
(
[0] => Nov
[1] => 66
)
[11] => Array
(
[0] => Dec
[1] => 78
)
)
</pre>
Print out "list":
Array
(
[Feb] => 3
)
Array
(
[Mar] => 6
)
Array
(
[Apr] => 10
)
Array
(
[May] => 15
)
Array
(
[Jun] => 21
)
Array
(
[Jul] => 28
)
Array
(
[Aug] => 36
)
Array
(
[Sep] => 45
)
Array
(
[Oct] => 55
)
Array
(
[Nov] => 66
)
Array
(
[Dec] => 78
)
Or print one array (month as key)
Array
(
[Jan] => 1
[Feb] => 3
[Mar] => 6
[Apr] => 10
[May] => 15
[Jun] => 21
[Jul] => 28
[Aug] => 36
[Sep] => 45
[Oct] => 55
[Nov] => 66
[Dec] => 78
)
BR
You may create an array filled from 1 to 12 and apply the array_reduce to get your output.
<?php
class P {
public $m1 = 1;
public $m2 = 1;
public $m3 = 1;
public $m4 = 1;
public $m5 = 1;
public $m6 = 1;
public $m7 = 1;
public $m8 = 1;
public $m9 = 1;
public $m10 = 1;
public $m11 = 1;
public $m12 = 1;
public function __construct() {
}
}
$ppmpitem = new P();
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$quantities = array_reduce($array, function ($carry, $item) use ($ppmpitem) {
$month = DateTime::createFromFormat('!m', $item)->format('M');
$carry['total'] += $ppmpitem->{"m" . $item};
$carry['quantities'][] = [$month, $carry['total']];
return $carry;
}, ['total' => 0, 'quantities' => []])['quantities'];
var_dump($quantities);
There are a number of ways to display a multidimensional array including use of the default PHP's print_r() function. Besides if you want a more precise way of accessing the elements and displaying them you can use a looping criteria such as for() or foreach(). Sample :
foreach($quantities as $qty){
print_r($qty);
}
i have 2 array and i want to merge or combine them...
Array
(
[0] => Array
(
[year] => 2015
[value] => 32
)
[1] => Array
(
[year] => 2016
[value] => 54
)
)
Array
(
[0] => Array
(
[year] => 2015
[value] => 95
)
[1] => Array
(
[year] => 2016
[value] => 2068
)
)
i want them to look like this...
Array(
[2015]=>array(
[0] => 32
[1] => 95
)
[2016]=>array(
[0] => 54
[1] => 2068
)
)
it this possible? if ever, how?.... thanks so much
$a = array(
0 => array
(
"year" => 2015,
"value" => 32
),
1 => array
(
"year" => 2016,
"value" => 54
)
);
$b = array(
0 => array
(
"year" => 2015,
"value" => 300
),
1 => array
(
"year" => 2016,
"value" => 5400
)
);
$c = array_merge($a,$b);
$output = array();
foreach($c as $key=>$val)
{
$output[$val['year']][] = $val['value'];
}
echo '<pre>';
print_r($output);
exit;
Try this code..
If the original arrays are $a and $b, run this code and the result you want will be in $result
$sources = array_merge($a,$b);
$result = [];
foreach($sources as $data){
$yr = $data['year'];
if(!isset($result[$yr])) $result[$yr]=[];
$result[$yr][]=$data['value'];
}
Live demo
Try:
$newArr = array();
foreach($array1 as $key1=>$arr1) {
$newArr[$arr1['year']][] = $arr1['value'];
$newArr[$arr1['year']][] = $array2[$key]['value'];
}
You can also do something like this,
<?php
$test1 = [["year"=>2015,"value"=>32],["year"=>2016,"value"=>54]];
$test2 = [["year"=>2015,"value"=>95],["year"=>2016,"value"=>2068]];
$newarray=array();
foreach($test1 as $key1=>$value1){
$temp = [$value1['value']];
foreach($test2 as $key2=>$value2){
if($value1['year']==$value2['year']){
$temp[] = $value2['value'];
}
$newarray[$value1['year']] = $temp;
}
}
print_r($newarray);
?>
check here : https://eval.in/605323
output is :
Array
(
[2015] => Array
(
[0] => 32
[1] => 95
)
[2016] => Array
(
[0] => 54
[1] => 2068
)
)
I have tried many things but could not get the output, would really appreciate any help
Thank you
Array (
[0] => Array ( [Toolrepos] =>
Array (
[id] => 28
[created] => 2014-12-13
[tool_type] => new1
[tool_partnum] => new3
[tool_vernum] => 57.0.5
[box_id] => 28
[request_date] => 2014-12-14
[delivered_date] => 2014-12-14 ) )
[1] => Array ( [Toolrepos] =>
Array (
[id] => 29
[created] => 2014-12-13
[tool_type] => new4
[tool_partnum] => new5
[tool_vernum] => 1.2.56
[box_id] => 28
[request_date] => 2014-12-14
[delivered_date] => 2014-12-14 ) )
[2] => Array ( [Toolrepos] =>
Array ( [id] => 29
[created] => 2014-12-13
[tool_type] => SeatApp
[tool_partnum] => sw2
[tool_vernum] => 1.1.2
[box_id] => 34
[request_date] => 2014-12-13
[delivered_date] => 2014-12-13 ) ) )
I need the output like below
if box_id = '28' then i need their corresponding values for 'created','tool_type','tool_vernum'. Sometimes I need only 'created' value for matching box_id. Thank you
$box28s = array();
$i=0;
if (! empty($arr)) {
foreach ($arr as $elem) {
$curr = ! empty($elem['Toolrepos']) ? $elem['Toolrepos'] : NULL;
if (! empty($curr)) {
foreach ($curr as $k => $v) {
if ($k == 'id' && $v == 28) {
$box28s[$i] = $curr;
}
}
}
++$i;
}
}
I have an array $matrix_part, containing arrays, and I want to rekey the inner keys to start at 1.
I am trying to code below but it doesn't work - it just stores the new array identically.
$temp_matrix = array();
foreach ($matrix_part as $k => $v){
$temp_matrix[$k++] = $v;
}
$matrix_part = $temp_matrix;
Source array:
Array
(
[0] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[1] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[2] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
)
Desired output:
Array
(
[0] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[1] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[2] => Array
(
[1] => 163
[1] => 23
[3] => 97
)
)
Try use:
instead of this:
$temp_matrix[$k++] = $v;
do this:
$temp_matrix[++$k] = $v;
This maybe?
$input = array(
array(163, 23, 97),
array(163, 23, 97),
array(163, 23, 97),
);
$output = array_map(function ($innerArray) {
return array_combine(range(1, sizeof($innerArray)), $innerArray);
}, $input);
print_r($output);
foreach ($a as $outer_k => $outer_v) {
for ($i = count($outer_v) - 1; $i >= 0; $i--) {
$outer_v[$i+1] = $outer_v[$i];
}
unset($outer_v[0]);
$a[$outer_k] = $outer_v;
}
where $a is your input array
Could do something like ...
foreach ($matrix as $k=>$v) {
foreach ($v as $k2=>$v2) {
$tmp_arr[$k][$k2+1] = $v2;
}
}
$matrix = $tmp_arr;