for loop -- ununderstandable behavior - php

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.

Related

How can I sum up values from a json string?

I get the following json data from a database:
[{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}]
I'm trying to add all value values together, so: 9.95 + 6.95 ... so that I get 67.6 as result.
I tried the below code, but I am getting 16.9 as repeated values.
for ($i = 0; $i <= $count - 1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
$totalservice = 0;
foreach ($serviceValue as $key => $value) {
$totalservice += $value['service_value'];
}
echo $totalservice;
}
You can do it like below:-
$jsonObj = json_decode($json); // Decode the JSON to OBJ
// Now loop and find the SUM
$total = 0;
foreach ($jsonObj as $item){
$total =+ $item->value;
}
// Print the SUM
echo "Sum : $total";
Note:- In your code $totalservice beome 0 every time when loop goes to next iteration and that's why you are getting same value repeated time. So do like (what #u_mulder said) :-
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
.....//rest code
}
I have made the below changes. It works fine.
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
foreach ($serviceValue as $key => $value) {
$totalservice+= $value['service_value'];
}
echo $totalservice;
}
Thanks for the help

warning occuring in php word occurence

I have written the following code to count the number of string occurrences in a given file.
PHP
<?php
$p = fopen("g.txt", "r");
$q = fread($p, filesize("g.txt"));
$t = explode(" ", $q);
$m = explode(" ", $q);
$i = 0;
$j = 0;
$r = 0;
$count = 0;
$c = count($t);
$d = array();
echo "count of".
"<br/>";
for ($i = 0; $i < $c; $i++) {
for ($j = $i; $j < $c; $j++) {
if ($t[$i] == $t[$j]) {
$count = $count + 1;
}
}
for ($r = $i + 1; $r < $c; $r++) {
if ($t[$i] == $t[$r])
unset($t[$r]);
}
echo $t[$i].
"=".$count.
"<br/>";
$count = 0;
}
?>
I am getting a notice of undefined offset on line numbers 17 and 24, though my output is coming out to be correct. Can you please help me in rectifying the above problem?
The problem is that you are deleting items from the array $t. You saved the count in $c, but the actual count will change by your last inner loop.
Even if you replace $c by count($t) everywhere, it will go wrong, because the last loop should be in reverse order, otherwise you skip items. For instance if you have the list 'a', 'b', 'c'. then when you delete 'b' and increment $r, you will not check 'c' at all.
So, if I fix those things, your code becomes as below. Although I didn't really check it for other issues. Frankly, I don't really get what is should do. ;-)
<?php
$p=fopen("g.txt","r");
$q=fread($p,filesize("g.txt"));
$t=explode(" ",$q);
$m=explode(" ",$q);
$i=0;
$j=0;
$r=0;
$count=0;
$d=array();
echo "count of"."<br/>";
for($i=0; $i<count($t); $i++)
{
for($j=$i; $j<count($t); $j++)
{
if($t[$i]==$t[$j])
{
$count=$count+1;
}
}
for($r=count($t) - 1; $r > $i; $r--)
{
if($t[$i]==$t[$r])
unset($t[$r]);
}
echo $t[$i]."=".$count."<br/>";
$count=0;
}
?>
In conclusion, you should do more tests. If the outcome of this script was okay, then it was by accident.

How do I simplify this basic loop?

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

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.

How to select a random set of rows?

How can I select a random set of rows
The important bits:
I need to specify the number of random rows to select via a variable.
Say for instance the number of rows I want to select is 10, then it HAS TO select 10 DIFFERENT rows. I don't want it to pick out the same row a few times until it has 10.
The code below picks out 1 random row, how can I tailor this to the above spec?
<?php $rows = get_field('repeater_field_name');
$row_count = count($rows);
$i = rand(0, $row_count - 1);
echo $rows[$i]['sub_field_name']; ?>
<?php
$rows = get_field('repeater_field_name');
$row_count = count($rows);
$rand_rows = array();
for ($i = 0; $i < min($row_count, 10); $i++) {
// Find an index we haven't used already (FYI - this will not scale
// well for large $row_count...)
$r = rand(0, $row_count - 1);
while (array_search($r, $rand_rows) !== false) {
$r = rand(0, $row_count - 1);
}
$rand_rows[] = $r;
echo $rows[$r]['sub_field_name'];
}
?>
This is a better implementation:
<?
$rows_i_want = 10;
$rows = get_field('repeater_field_name');
// Pull out 10 random rows
$rand = array_rand($rows, min(count($rows), $rows_i_want));
// Shuffle the array
shuffle($rand);
foreach ($rand as $row) {
echo $rows[$row]['sub_field_name'];
}
?>
Simply loop through the random row process the number of random rows you want to get.
<?php
$rows_to_get=10;
$rows = get_field('repeater_field_name');
$row_count = count($rows);
$x=0
while($x<$rows_to_get){
echo $rows[rand(0, $row_count - 1)]['sub_field_name'];
$x++;
}
?>
You can give this a try
$rows = get_field('repeater_field_name');
var_dump(__myRand($rows, 10));
function __myRand($rows, $total = 1) {
$rowCount = count($rows);
$output = array();
$x = 0;
$i = mt_rand(0, $rowCount - 1);
while ( $x < $total ) {
if (array_key_exists($i, $output)) {
$i = mt_rand(0, $rowCount - 1);
} else {
$output[$i] = $rows[$i]['sub_field_name'];
$x ++;
}
}
return $output ;
}
A simple solution :
$rows = get_field('repeater_field_name');
$limit = 10;
// build new array
$data = array();
foreach ($rows as $r) { $data[] = $r['sub_field_name']; }
shuffle($data);
$data = array_slice($data, 0, min(count($data), $limit));
foreach ($data as $val) {
// do what you want
echo $val;
}

Categories