How do I simplify this basic loop? - php

I have code which pretty much does this.....
//get the row info
$Row1 = $FullTable->find('div[class=ismPitchRow1]',0);
$Row2 = $FullTable->find('div[class=ismPitchRow2]',0);
$Row3 = $FullTable->find('div[class=ismPitchRow3]',0);
$Row4 = $FullTable->find('div[class=ismPitchRow4]',0);
$Row5 = $FullTable->find('div[class=ismPitchRow5]',0);
//Loop 5 times. One for each row on the pitch.
for ($i=1; $i<=5; $i++)
{
if ($i = 1) { echo $Row1; }
if ($i = 2) { echo $Row2; }
if ($i = 3) { echo $Row3; }
if ($i = 4) { echo $Row4; }
if ($i = 5) { echo $Row5; }
}
It works, but as you can see it's not very efficient and badly designed. How would I simplify this? I know there are much smaller ways that these kind of loops can be done.
Thanks.

use the great invention of arrays:
//get the row info
$Row[1] = $FullTable->find('div[class=ismPitchRow1]',0);
$Row[2] = $FullTable->find('div[class=ismPitchRow2]',0);
$Row[3] = $FullTable->find('div[class=ismPitchRow3]',0);
or, even more clever...
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
$Row[$i] = $FullTable->find($find,0);
}
do the same for echoing:
for ($i = 1; $i <= 5; $i++) {
echo $Row[$i];
}
but why not do everything in 1 loop?
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
echo $FullTable->find($find,0);
}

I would store $Row1 through $Row5 in an array and iterate through it with a foreach loop.
<?php
$YourArray = array();
array_push($YourArray,$FullTable->find('div[class=ismPitchRow1]',0),$FullTable->find('div[class=ismPitchRow2]',0),$FullTable->find('div[class=ismPitchRow3]',0),$FullTable->find('div[class=ismPitchRow4]',0),$FullTable->find('div[class=ismPitchRow5]',0));
foreach($YourArray as $row){
echo $row;
}
?>

Related

how to generate dynamic number array like below

$big_box = array(1,2,3,16,17,18,31,32,33,46,47,48,61,62,63,76,77,78,91,92,93,106,107,108,121,122,123,136,137,138............);
$small_box = array(4,19,34,49,64.............);
I want to generate dynamic numbers array like above example upto 10000.
For Array $big_box Try to something like this.
$j = 0;
for ($i=0; $i <= 1000; $i++) {
if ($i!=0) {
$big_box[] = $i;
$j++;
}
if ($j == 3) {
$j = 1;
$big_box[] = $i+13;
$i =$i+13;
}
}
echo "<pre>";
print_r($big_box);
And array $small_box
$small_box = array();
for($i=0; $i <= 1000; $i++) {
if ($i == 0) {
$small_box[$i] = 4;
}else{
$small_box[$i] = $small_box[$i-1]+15;
}
}
echo "<pre>";
print_r($small_box);

How to count how many duplicates an array has in Php

I'm new to Php and today I came across the rand()-function. I'd like to fill an array with numbers created with this function and then count the number of its duplicates. I already tried it a first time, but somehow I seem to be on the woodway.
<?php
$numbers = array();
for ($i=0; $i < 100; $i++) {
$numbers[$i] = rand(0, 100);
}
//$numbers = array(12,12,12,12);
echo "random numbers generated.<br>";
$arrLength = count($numbers);
$arrWithDoubles = array();
for ($i=0; $i < $arrLength; $i++) {
//echo "start looping for i: ".$i."! numbers['i'] has the content".$numbers[$i].".<br>";
for ($x=$i; $x < $arrLength; $x++) {
//echo "looped for x: ".$x."! numbers['x'] has the content".$numbers[$x].".<br>";
if($numbers[$i] == $numbers[$x]) {
if($i != $x) {
//echo "pushed to 'arrWithDoubles'.<br>";
array_push($arrWithDoubles, $numbers[$x]);
}
}
}
}
echo "numbers of doubles: ".count($arrWithDoubles)."<br>";
echo "list of numbers which were double:<br>";
for ($i=0; $i < count($arrWithDoubles); $i++) {
echo $arrWithDoubles[$i];
echo "<br>";
}
?>
The array_unique() function removes duplicates from an array, then just add a bit of math.
<?php
$numberOfDuplicates = count($orginalArray) - (count($orginalArray) - count(array_unique($originalArray)));
?>
$origin = array(2,4,5,4,6,2);
$count_origin = count($origin);
$unique = array_unique($origin);
$count_unique = count($unique);
$duplicates = $count_origin - $count_unique;
echo $duplicates;
$count = array();
foreach ($srcRandom as $sr) {
if (!array_key_exists ($sr, $count) ) {
$count[$sr] = 1;
continue;
}
$count[$sr]++;
}
var_dump ($count);
Thanks for all your input. With that I came to the following solution which fits my demand the best:
<?php
function countValueInArray($value, $array) {
$count = 0;
for ($i=0; $i < count($array); $i++) {
if($value == $array[$i]) {
$count++;
}
}
return $count;
}
$numbers = array();
for ($i=0; $i < 100; $i++) {
$numbers[$i] = rand(0, 100);
}
$duplicates = array();
for ($x=0; $x < count($numbers); $x++) {
$number = countValueInArray($numbers[$x], $numbers);
if ($number > 1) {
array_push($duplicates, $numbers[$x]);
}
}
$duplicatesList = array_values(array_unique($duplicates));
echo "number of duplicates: ".count($duplicatesList);
echo "<br>these are: <br>";
print_r($duplicatesList);
?>
Thanks a lot for your help!

for loop -- ununderstandable behavior

I have have these for loops:
for ($x = 0; $x<2;$x++){
$allproducts = array();
for ($y = 0; $y<105;$y++) { //<<<<<<<<<<<<< this (105) is the number I mean
$allproducts[] = 'test'.$y;
}
echo "<pre>";
echo "allproducts 0 -- ";
var_dump($allproducts);
echo "</pre>";
for ($i = 0; $i < count($allproducts); $i++) {
$result = array_slice($allproducts, 0, 20);
echo "<pre>";
echo "result -- ";
var_dump($result);
echo "</pre>";
$allproducts = (array_diff($allproducts, $result));
echo "<pre>";
echo "allproducts 1 -- ";
var_dump($allproducts);
echo "</pre>";
}}
In real code (here:)
for ($x = 0; $x<1;){
$allproducts = array();
$abfrage = "SELECT sku FROM skulist";
if ($result = $sql->query($abfrage))
{
while ($row = $result->fetch_assoc()) {
$allproducts[] = $row['sku'];
}
}
for ($i = 0; $i < count($allproducts); ++$i) {
$result = array_slice($allproducts, 0, 20);
$allproducts = (array_diff($allproducts, $result));
//.... more code
}}
...I take data from DB instead of this for loop:
$allproducts = array();
for ($y = 0; $y<105;$y++) { //<<<<<<<<<<<<< this (105) is the number I mean
$allproducts[] = 'test'.$y;
}
but changed it to show/ for easier reconstruction.
The behavior is the same:
When I have 105 entries in my array, I don't get an empty array output for
echo "allproducts 1 -- ";
var_dump($allproducts);
which is the desired behavior.
When I change 105 to (eg.) 65, it outputs an empty arrays
allproducts 1 -- array(0)
at the end of the second nested loop:
for ($i = 0; $i < count($allproducts); $i++) {
Now I have three problems:
why does this happen? I don't get the difference
how can I achieve, that there is no empty array at the end? (stops my programs work)
I need 20 values in every $result array
for ($i = 0; $i < count($allproducts); $i++) {
// ...
$allproducts = (array_diff($allproducts, $result));
You are changing your $allproducts array while going through it! How can you expect it to work reliably then?
Instead, try:
foreach( $allproducts as $i => $product)
Although I must admit I'm not sure what you're trying to do.

print number vertically and grouping it

I am trying to print number vertically and it must be in group
here is my code
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= 24; $j = $j + $rows) {
$count++;
if($count>$nums){
break;
}
echo "<div class='fleft'>$count</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
out of above
but i want output like for the first column
and next group number will start from where first group number end. in this case next group start from 25
please ask if any doubt
$nums = 105;
$rows = 8;
$colsize = 3;
$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
$modulo = 0;
$correction = 0;
$rest = $nums - $count;
if ($rest < $groupsize) {
$empty = $groupsize - $rest;
$correction = floor($empty / $colsize);
$modulo = $empty % $colsize;
}
for ($i = 1; $i <= $rows; $i++) {
$colind = 0;
for ($j = $i; $j <= $groupsize; $j = $j + $rows) {
$count++;
if ($count > $nums) {
break;
}
$val = $j + ($g * $groupsize);
$val -= $colind * $correction;
$modcor = $colind - ($colsize - $modulo);
if ( $modcor > 0 ) {
$val -= $modcor;
}
echo "<div class='fleft'>" . $val . "</div>";
$colind++;
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
This works:
Also, you can change number of digits, columns or size of column
for($group = 0; $group < 3; $group++){
for($row =1 ; $row <= 8; $row++){
for($col = 0; $col <= 2; $col++){
echo ($group*24)+ $row + 8 * $col; echo " ";
}
echo "\n";
}
}
This code will print the number in the requested format. You need to modify according to your need.
may be i am mad , made a simple alter .... try this
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0; //added a counter
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
$letCounter=0; //reset counter on each loop
for ($j = $i; $j <= 24; $j = $j + $rows)
{
$count++;
if($count>$nums)
{break;}
//made an alter in the below line , some math :)
echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
Thanks !
This May work
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{
if($c%$group== 1)
{
echo "Group Start";
$flag = false;
}
for ($i = 1; $i <= $rows; $i++) {
$count++;
echo "<div class='fleft'>$count</div>";
echo "<div class='clear'></div>";
}
echo "Line End";
if($c%$group == 2&& $flag)// Check here for your requirement
echo "Group End </br>";
$flag = true;
}

PHP: Cannot use [] for reading - but it's not [] but [$counter]

in general I think I understand what the error message means. But in my case, it's a riddle I didn't succeed in solving...
$keywords_all = array();
$count = 0;
for ($z = 0; $z < $num_results; $z++)
{
$keywords_array = explode(",", $row['free_keywords']);
for ($i = 0; $i < count($keywords_array); $i++)
{
if (in_array(strtolower(trim($keywords_array[$i])), $keywords_all))
{
$count++;
}
else
{
echo "<br />".$keywords_array[$i];
$keywords_all[$count] = $keywords_array[$i];
}
}
$row = pg_fetch_array($result);
}
So, what's wrong with that one? The error message pops up in the line
$keywords_all[$count] = $keywords_array[$i];
I have no clue, seems to be alright to me. But guess, it's again a tiny, tiny thing I've neglected... Thanks for any hints!
I was not able to reproduce your error message. I did find a bug in your code though (I am assuming that you are putting all your keywords in the $keywords_all array without any duplicates). So you should not increment $count inside your IF but instead update the $keywords_all count. See below:
if (in_array(strtolower(trim($keywords_array[$i])), $keywords_all)) {
$count = count($keywords_all);
} else {
echo "<br />".$keywords_array[$i];
$keywords_all[$count] = $keywords_array[$i];
$count++;
}
You will increment $count after storing a value to your $keywords_all array.
$keywords_all = array();
$count = 0; // what for you neet this var ?
$myRow = 'keyW1,keyW2,keyW3,keyW2';
// for ($z = 0; $z < $num_results; $z++) // there is no variable $num_results at your code so I've replaced it with constant
for ($z = 0; $z < 1; $z++)
{
// $keywords_array = explode(",", $row['free_keywords']);
$keywords_array = explode(",", $myRow);
// for ($i = 0; $i < count($keywords_array); $i++)
foreach ($keywords_array as $keyword)
{
$keyword = strtolower( trim( $keyword ) ); // some syntax sugar would be nice
if ( !in_array( $keyword, $keywords_all) )
{
echo "<br />".$keyword;
$keywords_all[] = $keyword;
}
}
// $row = pg_fetch_array($result);
}
var_dump($keywords_all);
This code would be better for you i think, but if you just want to get rid of duplicated records
array_uniq( array("1", "1", "2", "1") )
would be better solution for you.

Categories