php counter is being reset each loop - php

I've got a rather complicated set of loops that pulls data out of mysql and compares it to values in an array and increments a counter. When I echo a flag when the counter is incremented, I get a bijilion flags (there're like 2600 records returned from the mysql query). But each time it prints, the counters are always 1 and when I print the counter's value at the end, it shows up as zero. It seems like something is re-setting the counter…
code
# ARRAY
$demographics=array(
"region"=>array(
"Northeast"=>array('total'=>0,'consented'=>0,'completed'=>0),
//more...
"West"=>array('total'=>0,'consented'=>0,'completed'=>0)
),"societal envirn"=>array(
"Urban"=>array('total'=>0,'consented'=>0,'completed'=>0)
),"age"=>array(
'18-19'=>array('total'=>0,'consented'=>0,'completed'=>0),
'20-24'=>array('total'=>0,'consented'=>0,'completed'=>0),
//more...
'55-59'=>array('total'=>0,'consented'=>0,'completed'=>0)
),
//more...
);
# LOOPS
while ($dbrecord = mysql_fetch_assoc($surveydata)) {
foreach ( $dbrecord as $dbfield=>$dbcellval ) {
foreach ( $demographics as $demographic=>$options ) {
foreach ( $options as $option=>&$counter ) {
if($demographic==="age"){
list($min,$max) = explode('-', $option);
if ($dbcellval >= $min && $dbcellval <= $max){
$counter['total']++;
echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
if ($dbrecord['consent']==="1"){
$counter['consented']++;
echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
if ($dbrecord['completion status']==="complete") {
$counter['completed']++;
echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
break 3;
} // if
} // if
break 2;
}
} // if age
else if ($option===$dbcellval){
$counter['total']++;
echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
if ($dbrecord['consent']==="1"){
$counter['consented']++;
echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
if ($dbrecord['completion status']==="complete") {
$counter['completed']++;
echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
break 3;
} // if
} // if
break 2;
} // else if $option==$dbcellval
} // foreach $options
} // foreach $demographics
} // foreach $dbrecord
} // while
sample output
$40-44['total'] = 1
$White['total'] = 1
$35-39['total'] = 1
$Northeast['total'] = 1 // the 'total' counter is 1
$Northeast['consented'] = 1
$Northeast['completed'] = 1
$South['total'] = 1
$Northeast['total'] = 1 // notice the 'total' counter is 1 again :(
$Northeast['consented'] = 1
$Northeast['completed'] = 1

You're defining counter from a foreach instruction, as $value in foreach($foo as $key=>$value), when using the foreach you only have a local copy of $counter.
You need to use either foreach($foo as $key=>&$value) or to refer to the full array path of your counter from $demographics.

You need to reference your array at each level, otherwise you are working on a copy of the data:
foreach ( $dbrecord as $dbfield=>$dbcellval ) {
foreach ( $demographics as $demographic => &$options ) {
foreach ( $options as $option => &$counter ) {
if($demographic==="age"){
list($min,$max) = explode('-', $option);
if ($dbcellval >= $min && $dbcellval <= $max){
$counter['total']++;

What if you simply stick with the $demographics variable.
i.e.
...
foreach ( $options as $option ) {
...
$demographics[$option]['total']++;
...

Related

How to count/sum this values of the same word in PHP?

I am confused to count these words,
I've some data like this :
web = 1
sistem=1
web=1
sistem=1
web=1
sistem=1
sistem=0
sistem=0
web=0
sistem=0
web=0
sistem=0
web=0
web=0
I want to make result like this :
web = 3
sistem = 3
I'm using array_count_values(), but this result is not good
Array ( [web=1] => 3 [sistem=1] => 3 [sistem=0] => 4 [web=0] => 4 )
My code like this :
foreach ($g as $key => $kata) {
if (strpos($cleanAbstrak, $kata)) {
echo $kata . $ada . "<br>";
$p[] = $kata . "=" . $ada;
// print_r($p);
echo "<br><br>";
} else {
echo $kata, $tidak . "<br>";
$q[] = $kata . "=" . $tidak;
// $m = explode(" ", $q);
// print_r($q);
// echo $q . '<br>';
echo "<br><br>";
}
}
$s = array_merge($p, $q);
echo "<br><br>";
print_r($s);
echo "<br>";
$f = array_count_values($s);
// print_r($f);
echo "<br><br>";
thank you very much if you are willing to help me
RESULT THIS CODE
Another simple way is use a counter like that:
$web=0;
$sistem=0;
foreach ($g as $key => $kata) {
if (strpos($cleanAbstrak, $kata)) {
$sistem=$sistem + $ada;
} else {
$web=$web+$tidak
}
}
echo 'web='.$web.'<br> sistem='.$sistem;
First, you need to separate word and value.
Second, you need to check the value : if it's zero you let it go (can't hold it back anymore). Else you count the value ; if it's written, i suppose it can be greater than 1 ; if it's not, it should be "word", or nothing (which would greatly facilitate counting).
Something like
<?php
$tab = [
'web=1',
'sistem=1',
'web=1',
'sistem=1',
'web=1',
'sistem=1',
'sistem=0',
'sistem=0',
'web=0',
'sistem=0',
'web=0',
'sistem=0',
'web=0',
'web=0',
];
$tab_clean = [];
foreach($tab as $item) {
preg_match('/([a-z]+)=([\d]+)/', $item, $matches);
//print_r($matches);
$word = $matches[1];
$number = $matches[2];
for($i = 0; $i < $number; $i++) {
$tab_clean[] = $word;
}
}
$tab_count = array_count_values($tab_clean);
print_r($tab_count);
?>

Compare each index in single array

I have a function to calculate distance, the data comes from a database.
Here is the code for calculating:
function jarak() {
global $conn;
$query1 = mysqli_query($conn, "SELECT signature, sig_priority FROM centro");
$query2 = mysqli_query($conn, "SELECT signature, sig_priority, status FROM acid_event");
while ($row = mysqli_fetch_array($query1, MYSQLI_ASSOC)) { $master[]=$row; }
while ($row = mysqli_fetch_array($query2, MYSQLI_ASSOC)) { $data[]=$row; }
$jarak = array();
foreach ($data as $key => $val) {
foreach ($master as $key2 => $value) {
$jarak = sprintf("%0.2f",sqrt(pow($val['signature'] - $value['signature'], 2) + pow($val['sig_priority'] - $value['sig_priority'], 2)));
echo "distance from (" . $value['signature'] . "," . $value['sig_priority'] . ") ke (" . $val['signature'] . "," . $val['sig_priority'] . ") is : " . $jarak . "<br>";
$euc[]=$jarak;
}
}
}
And here is the result from that:
Array(
[0] => 30.04
[1] => 0.00
[2] => 30.04
[3] => 0.00
[4] => 47.00
[5] => 17.03
[6] => 5.02
[7] => 25.08
[8] => 2.06
[9] => 32.06
[10] => 37.00
[11] => 7.07 )
I want to compare each 2 index array with greater than or less than.
Example : [0] with [1], [2] with [3], [4] with [5] and so on. It just compare with 2 index.
I tried this but no result
for ($i=0; $i<count($cb); $i++) {
for ($k=0;$k<2;$k++) {
if($cb[$i][$k]<$cb[$i][$k]) {
echo "low";
} elseif ($cb[$i][$k]>$cb[$i][$k]) {
echo "high";
}
}
}
Output I want should look like this
if [0] < [1] then "high" and it loop for other index array like [2] with [3], [4] with [5] and so on.
I think you were quite close to what you wanted to accomplish but it seems like you were making things harder than they needed to be.
Your code is below.
for ($i=0; $i<count($cb); $i++) {
for ($k=0;$k<2;$k++) {
if($cb[$i][$k]<$cb[$i][$k]) {
echo "low";
} elseif ($cb[$i][$k]>$cb[$i][$k]) {
echo "high";
}
}
}
As you can see your if statements are comparing the exact same value to each other, that's not going to do much. But I can see what you were trying to do in the second for loop.
Instead, what we really want to do is move through your array in steps of 2.
for ($i=0; $i<count($cb); $i+=2) {
//stuff
}
This way we can compare the first element and the element after that to each other. Like this:
if($cb[$i] > $cb[$i+1]) {
echo $i . 'is higher than '. $i+1;
} elseif($cb[$i] < $cb[$i+1]) {
echo $i . 'is lower than '. $i+1;
} else {
echo $i . 'is the same as '. $i+1;
}
So all together it would be something like this:
for ($i=0; $i<count($cb); $i+=2) {
if($cb[$i] > $cb[$i+1]) {
echo $i . 'is higher than '. $i+1;
} elseif($cb[$i] < $cb[$i+1]) {
echo $i . 'is lower than '. $i+1;
} else {
echo $i . 'is the same as '. $i+1;
}
}
Now you can change the echo's to whatever you want to do and you should probably add some validation too (like checking if the keys actually exist before accessing them), but this is a good place to get started.
I did a solution, but I decided to keep the response in an array because it makes more sense to me to be able to read this information somewhere else later.
Here's my solution:
$test = array(
30.04,
0.00,
30.04,
0.00,
47.00,
17.03,
5.02,
25.08,
2.06,
32.06,
37.00,
7.07,
);
$output = array();
foreach ($test as $key => $value) {
// To make sure this will only be executed every second item
if ($key % 2 !== 0) {
continue;
}
$next = '';
if (!isset($test[$key+1])) {
break;
} else {
$next = $test[$key+1];
}
$output[$key] = $value . ' is ' . ($value < $next
? "lower"
: ($value > $next
? 'higher'
: 'equal')) . ' than ' . $next;
}
echo '<pre>';
print_r($output);
Here's the code tested: https://3v4l.org/Pg5La

How to print just a few from an array instead of all in php

This is part of the working code that shows the entire array;
$files = filelist("./",1,1); // call the function
shuffle($files);
foreach ($files as $list) {//print array
echo "<h4> " . $list['name'] . " </h4>";
// echo "Directory: " . $list['dir'] . " => Level: " . $list['level'] . " => Name: " . $list['name'] . " => Path: " . $list['path'] ."<br>";
How do I modify it so that it only displays 10 or 15 list instead of all?
Use a counter to limit the number of iterations:
$counter = 0;
foreach ($files as $list) {//print array
// your loop code here...
$counter++;
if ($counter > 10) break;
}
If you know the keys or indexes of the array you can do what KingCrunch is doing a lot faster by simple for loop
for($i=0; $i<=14; $i++) {
// echo $file[$i];
}
There is a function for it
foreach(array_slice($files, 0, 15) as $file) {
/* your code here */
}
http://php.net/array-slice
Another solution is to use array_rand() instead of shuffle() and array_chunk()
foreach (array_rand($files, 15) as $key) {
$file = $files[$key];
// Your code here
}
http://php.net/array-rand
Note, that this keeps the order of the keys (see salathes comment below).

To get the multi dimention array values in php

I have a multi-dimension array in php like this
$shop = array(
array("name","point","number"),
array('Ranjit', 1.25 , 15),
array('Pitabas', 0.75 , 25),
array('Khela', 1.15 , 7)
);
Now I have to show the output like this
name-> ranjit
Point-> 1.25
number->15
name->Pitabas
Point->0.75
number->25
name->Khela
Point->1.15
number->7
I am trying for loop, but I could get the result in nested forloop. Please help me to get the answer.
My solution:
$headings = array_shift($shop);
foreach ($shop as $item) {
foreach ($item as $key => $value) {
echo $headings[$key], '=>', $value;
}
}
Here's a simple loop: Observe that we skip the first element of the outer array, which is deemed to contain the headers:
for ($i = 1; $i != count($shop); ++$i)
{
print $shop[0][0] . ": ". $shop[$i][0] . "\n";
print $shop[0][1] . ": ". $shop[$i][1] . "\n";
print $shop[0][2] . ": ". $shop[$i][2] . "\n";
}
You know the first row will be the titles, so store them separately:
$titles = $shop[0];
That will give you
$titles = array('name', 'point', 'number');
Then loop through your array:
foreach ($shop as $index => $row) {
if ($index == 0)
continue;
foreach($row as $column => $item) {
echo $titles[$column] . " -> " . $item . "<br />";
}
}
This should give the desired output:
for($x = 1, $lim = sizeof($shop); $x < $lim; $x++)
{
echo $shop[0][0]."->".$shop[$x][0]."<br>";
echo $shop[0][1]."->".$shop[$x][1]."<br>";
echo $shop[0][2]."->".$shop[$x][2]."<br>";
}

Looping through array objects to output

I have sent $data['items'] to my view which has created an array full of objects which I can echo with a foreach loop.
foreach($items as $row)
{
echo $row->NAME . " - " . $row->COLOUR . "<br>";
}
what I want to do is echo them to the browser in groups with the name of the colour as a header tag then start the loop for that colour. I'm just not sure what type of loop to do or should I have a loop within a loop?
BLUE
-item 1
-item 3
RED
-item 2
-item 4
-item 5
$list = array();
foreach($items as $row)
{
$list[$row->COLOUR][] = $row->NAME;
}
$header = null;
foreach($list as $item)
{
if($header != $item->COLOUR)
{
echo '<h3>' . $item->COLOUR . '</h3>';
$header = $item->COLOUR;
}
echo '- ' . $item->NAME . '<br />';
}
You probably want an interim two-dimensional array:
$tmp = array();
foreach($items as $row)
{
// this code groups all items by color
$name = $row->NAME;
if( !isset ($tmp[ $name ] ) ) $tmp[ $name ] = array();
$tmp[ $name ][] = $row->COLOUR;
}
foreach( $tmp as $color => $items )
{
// colors are now keys to the temp array
echo $color;
// these are all of the items grouped under the current color
foreach( $items as $item )
{
// output the item.
echo "<br /> - $item";
}
echo "<br />";
}

Categories