Looking for help in counting elements in a php array meeting certain criteria and getting them to properly display in html table.
I have this array named $Array
Array
(
[0] => Array
(
[fMonth] => 12
[fSnowDepth] => 0.2
)
[1] => Array
(
[fMonth] => 12
[fSnowDepth] => 3.7
)
[2] => Array
(
[fMonth] => 12
[fSnowDepth] => 1
)
[3] => Array
(
[fMonth] => 01
[fSnowDepth] => 1
)
[4] => Array
(
[fMonth] => 01
[fSnowDepth] => 0.5
)
[5] => Array
(
[fMonth] => 01
[fSnowDepth] => 4.5
)
[6] => Array
(
[fMonth] => 01
[fSnowDepth] => 1.3
)
)
What I'm trying to do is count the months which meet conditions such as fSnowDepth >= 1 and < 3, fSnowDepth >= 3 and < 5, etc. and place in html table. I'm expecting this with (blank) for the months with no count:
| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
SD >=1 and < 3 | 2 | | | | | | | | | | | 1
SD >=3 and < 5 | 1 | | | | | | | | | | | 1
... etc depths | | | | | | | | | | | |
The code I have is:
$array = $result['rawSumSnowDepth'];
foreach ($array as $key => $items) {
if ($items['fSnowDepth'] >= 1 && $items['fSnowDepth'] < 3) {
$value = $items['fMonth'];
$output[$value] = ($output[$value] ?? 0) + 1;
for ($x = 0; $x <= 11; $x++) {
if ($x + 1 == $items['fMonth']) {
$result['snDaysOverAmt'][$x] = array($output[$value]);
} elseif (empty($result['snDaysOverAmt'][$x])) {
$result['snDaysOverAmt'][$x] = array($output[$value] => " ");
}
}
}
}
if (isset($result['snDaysOverAmt'])) {
foreach ($result['snDaysOverAmt'] as $amounts => $amount) {
if ($amount) {
echo '<td>' . implode($amount) . '</td>';
}
}
}
This code works like a charm for the first row of snow depths >= 1 and < 3 but when I run the code again for the next row (>= 3 and < 5) and I get what appears to be a doubling of the first row.
Is there another way to do this so I can include different snow depth counts AND is there a more concise way of doing this? I'm still a PHP rookie so any help would be appreciated.
Thanks to #Barmar this was all that was needed:
$output = [];
$result['snDaysOverAmt'] = [];
these were placed after the last closing bracket in the code provided in the question above.
Related
I have this table data:
Date | Room | Reading | Pax
2019-05-27 | B-1 | 1000 | 1
2019-05-29 | B-1 | 1010 | 1
2019-05-31 | B-1 | 1020 | 1
2019-06-02 | B-1 | 1030 | 2
2019-06-04 | B-1 | 1040 | 2
2019-06-06 | B-1 | 1050 | 2
2019-06-08 | B-1 | 1060 | 2
2019-06-10 | B-1 | 1070 | 1
2019-06-12 | B-1 | 1080 | 1
What I wanted to do is to group them up like this:
$my_array();
foreach ($results as $result) {
//group them up by room first, then by pax, then by dates
$my_array[$result->room][$result->pax][$result->date] = array('reading'=>$result->reading);
}
What out I expect is:
Array
(
[B-1] => Array
(
[1] => Array
(
[2019-05-27] => Array
(
[reading] => 1000
)
[2019-05-29] => Array
(
[reading] => 1010
)
[2019-05-31] => Array
(
[reading] => 1020
)
)
[2] => Array
(
[2019-06-02] => Array
(
[reading] => 1030
)
[2019-06-04] => Array
(
[reading] => 1040
)
[2019-06-06] => Array
(
[reading] => 1050
)
[2019-06-08] => Array
(
[reading] => 1060
)
)
[1] => Array
(
[2019-06-10] => Array
(
[reading] => 1070
)
[2019-06-12] => Array
(
[reading] => 1080
)
[2019-05-31] => Array
(
[reading] => 1020
)
)
)
)
But all the data with same room and same pax are together, I wanted to seperate them by the output I expected, because I will be counting the number of readings base on the # of pax and the dates between, so from 2019-05-27 to 2019-05-31 is 1020 - 1000 = 20 / 1 = 20 and from 2019-06-02 to 2019-06-08 is 1060 - 1030 = 30 / 2 = 15 and from 2019-06-10 to 2019-06-12 is 1080 - 1070 = 10 / 1 = 10 and I will add everything. How will I be able to get them grouped properly
<?php
// init data
$results = array(
array("date"=>"2019-05-27","room"=>"B-1","reading"=>1000,"pax"=>"1"),
array("date"=>"2019-05-29","room"=>"B-1","reading"=>1010,"pax"=>"1"),
array("date"=>"2019-05-31","room"=>"B-1","reading"=>1020,"pax"=>"1"),
array("date"=>"2019-06-02","room"=>"B-1","reading"=>1030,"pax"=>"2"),
array("date"=>"2019-06-04","room"=>"B-1","reading"=>1040,"pax"=>"2"),
array("date"=>"2019-06-06","room"=>"B-1","reading"=>1050,"pax"=>"2"),
array("date"=>"2019-06-08","room"=>"B-1","reading"=>1060,"pax"=>"2"),
array("date"=>"2019-06-10","room"=>"B-1","reading"=>1070,"pax"=>"1"),
array("date"=>"2019-06-12","room"=>"B-1","reading"=>1080,"pax"=>"1"),
);
$my_array = array();
$pax_current = 0;
$no = 0;
foreach ($results as $result) {
// increment N-array when pax change
if ($pax_current != $result['pax']) {$no++; $pax_current=$result['pax'];}
//group them up by room first, then by change pax, then by dates
$my_array[$result['room']][$no][$result['date']] = array('reading'=>$result['reading'],'pax'=>$result['pax']);
}
//Output
print("<pre>");
print_r($my_array);
print("</pre>");
?>
Using the query builder to try and select rows where a value in the HSTORE key $type is present.
My Where Statement:
->where("meta_data_fields->'$type'", 'like', '%'.$query.'%')
Not sure what I'm doing wrong. I get this error:
Undefined column: 7 ERROR: column "meta_data_fields->'Entity::hstore'" does not exist
Any ideas?
to get all records in table that have HSTORE
Model::where("meta_data_fields", 'like', '%'.$query.'%')->get();
This should do normally do the trick. In your case, data in meta_data_fields is a json string. if the search is performed on all rows in the table, will select all. So here is What I did,
I created a queryScope in the model
1 - fetch all data
2 - extract meta_data_field and decode it
3 - loop through meta_data_field
4 - select only that match the criteria and build the array
here is a table
+----+-------------+------------------------------------+-----------+
| id | name | desc | vendor_id |
+----+-------------+------------------------------------+-----------+
| 1 | apples | {"type":"fruit","origin":"mexico"} | 1 |
| 2 | oranges | {"type":"fruit","origin":"peru"} | 1 |
| 3 | Peaches | {"type":"fruit","origin":"mexico"} | 2 |
| 4 | Cherries | {"type":"fruit","origin":"us"} | 1 |
| 5 | banans | {"type":"fruit","origin":"brazil"} | NULL |
| 6 | Water Melon | {"type":"fruit","origin":"mexico"} | 1 |
+----+-------------+------------------------------------+-----------+
public function scopeItems($name ="mexico"){
$items = array();
$data = Self::get();
$meta_fields = json_decode($data -> pluck('desc') ,true);
foreach ($meta_fields as $key => $value) {
if (isset($value['origin']) && $value['origin'] == $name ){
$items[$key] = $data[$key] -> toArray();
}
}
return $items;
}
// output
Array
(
[0] => Array
(
[id] => 1
[name] => apples
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 1
)
[2] => Array
(
[id] => 3
[name] => Peaches
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 2
)
[5] => Array
(
[id] => 6
[name] => Water Melon
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 1
)
)
Hope this works for you this time.
So here's what worked in my situation with Laravel.
$result = \DB::select(\DB::raw("SELECT hstore_fields, id from table where lower(hstore_fields->'$type') like lower('%$query%')"));
I have this table in my DB (I hope it's correctly showed):
+++++++++++++++++++++++++++
| id_child |--| id_parent |
+++++++++++++++++++++++++++
| 5 |--| 2 |
| 6 |--| 2 |
| 7 |--| 4 |
| 8 |--| 4 |
| 9 |--| 5 |
| 10 |--| 5 |
| 11 |--| 9 |
| 12 |--| 9 |
---------------------------
I wrote a php recursive function that create a multidimensional array from a parent passed (in this case '2').
So, if I put a print_r I obtain this result:
Array ( [5] => Array ( [9] => Array ( [11] => Array ( ) [12] => Array ( ) ) [10] => Array ( ) ) [6] => Array ( ) )
How I can obtain a structure of this type?
(I exclude the first parent, 2)
(2)
-5
--9
----11
----12
--10
-6
Thanks.
You would need another recursive function to iterate over your array, like so:
function printCategories($categories, $level = 1) {
foreach ($categories as $cat => $subCats) {
echo str_repeat('-', $level) . $cat . "\n";
printCategories($subCats, $level+1);
}
}
printCategories($categories);
<?php
function printtree($tree, $level) {
$prefix=str_repeat('-',$level);
foreach ($tree as $k=>$v) {
echo "$prefix$k<br>\n";
if (is_array($v)) if (sizeof($v)>0) printtree($v,$level+1);
}
}
$tree=array( 5=>array(9=>array(11=>array(), 12=>array()), 10=>array()), 6=>array());
printtree($tree,1);
?>
I wanna count idf, the formula is IDF=log(D/df) that D is total data and df is many data that contain the searched words.
from the tables :
1. tb_stemming
===========================================================================
|stem_id | stem_before | stem_after | stem_freq | sentence_id |document_id|
===========================================================================
| 1 | Data | Data | 1 | 0 | 1 |
| 2 | Discuss | Discuss | 1 | 1 | 1 |
| 3 | Mining | Min | 1 | 0 | 2 |
===========================================================================
here's the code :
countIDF($total_sentence,$doc_id);
that $total_sentence is
Array ( [0] => 644 [1] => 79 [2] => 264 [3] => 441 [4] => 502 [5] => 18 [6] => 352 [7] => 219 [8] => 219 )
function countIDF($total_sentence, $doc_id) {
foreach ($total_sentence as $doc_id => $total_sentences){
$idf = 0;
$query1 = mysql_query("SELECT document_id, DISTINCT(stem_after) AS unique_token FROM tb_stemming group by stem_after where document_id='$doc_id' ' ");
while ($row = mysql_fetch_array($query)) {
$token = $row['unique_token'];
$doc_id = $row['document_id'];
$ndw = countNDW($token);
$idf = log($total_sentences / $ndw)+1;
$q = mysql_query("INSERT INTO tb_idf VALUES ('','$doc_id','$token','$ndw','$idf') ");
}
}
}
and the function of countNDW is :
function countNDW ($word) {
$query = mysql_query("SELECT stem_after, COUNT( DISTINCT sentence_id ) AS ndw FROM `tb_stemming` WHERE stem_after = '$word' GROUP BY stem_after");
while ($row = mysql_fetch_array($query)) {
$ndw = $row['ndw'];
}
return $ndw;
}
It can't worked well, especially in call from database. All I need is to count in every document_id. How to define it in my code ? please, help me.. thank you so much :)
So I have an array like the following:
Array
(
[0] => Array
(
[user_id] => 684
[sec_id] => 2
[rank_id] => 1
[rank] => usr
)
[1] => Array
(
[user_id] => 693
[sec_id] => 3
[rank_id] => 5
[rank] => usr
)
)
And I have another array like this
Array
(
[0] => 2
[1] => 7
[2] => 27
)
I want the value of the second array to be added at the end of each arrays of the 1st array, and it should be multiplied. I mean, if I have 100 arrays in the first array, and 3 elements in the second array, I should have 300 in the resulting array.
Taking example of the above, I would like to have something as follows:
user_id | sec_id | rank_id | rank | menu_id
684 | 2 | 1 | usr | 2
684 | 2 | 1 | usr | 7
684 | 2 | 1 | usr | 27
693 | 3 | 5 | usr | 2
693 | 3 | 5 | usr | 7
693 | 3 | 5 | usr | 27
I tried with the following function, but it's not working.
function getR($arr_one,$arr_two) {
foreach ($arr_one as $k=>&$v) {
foreach ($arr_two as $x=>&$y) { $v['menu_id'] = $y; }
}
return $arr_one;
}
This is just making an array like this:
user_id | sec_id | rank_id | rank | menu_id
684 | 2 | 1 | usr | 27
693 | 3 | 5 | usr | 27
Means, it's just adding menu_id at the end of each element of the first array, but not multiplying. Any idea, I'm surely missing something.
Thanks guys.
function getR($arr_one,$arr_two) {
$new_arr = array();
foreach ($arr_one as $k=>$v) {
foreach ($arr_two as $x=>$y) {
$this_item = $v;
$this_item['menu_id'] = $y;
$new_arr[] = $this_item;
}
}
return $new_arr;
}
I'm not going to ask... but try this:
<?php
function crazy ($arr1,$arr2) {
foreach ($arr1 as $key=>$value) {
foreach ($arr2 as $value2) {
$nvalue=$value;
$nvalue[]=$value2;
$new[]=$nvalue;
}
}
return $new;
}
$arr1=array(array('user'=>1,'dude'=>2),array('user'=>2,'dude'=>3));
$arr2=array(2,7,27);
print_r(crazy($arr1,$arr2));
this is tested too, http://www.ideone.com/Of126
Without testing (eek!) I imagine something like this:
function getR( $arr_one, $arr_two )
{
$second_key = 0;
foreach ( $arr_one as $k => &$v )
{
$v['menu_id'] = $second_key++;
if ( 3 == $second_key ) $second_key = 0;
}
return $arr;
}
Presumably, you're passing the first array by reference? Not sure what $arr is that you're returning though...