At the moment i try to create a function in php which simply reads an int value of a field of an array and compares it to a max value.
If this max value is reached, it shall set the value of the field to zero, jump to the next field in the array and increase the int value stored inside.
If this value also reached the max value do the same as above.
If the above condition isnĀ“t true it shall just increase the stored value in the array.
My Code looks like this:
if($sign_counter[0] === (count($pw_signs) - 1)){
$counter = 0;
while($sign_counter[$counter] === (count($pw_signs) - 1)){
$sign_counter[$counter] = "0";
$counter++;
}
$sign_counter[$counter]++;
}
else{
$sign_counter[0]++;
}
I allready tested this part of the function several times with different values on my website and browser. I also checked if the values were stored correctly in the array and inside needed variables.
Thats how my array looks like:
$sign_counter = array("38", "2");
For example:
$sign_counter array to store int values
(count($pw_signs) - 1) always equals 38 (because there are 39 fields in the counted array)
$counter used to determine the field position inside the array
Now if i store the value "38" in the first field and "2" in the second field of the array the code should detect, that the max value is reached in the first field, set the value of the field to 0, then jump to the next field and increase its value by 1.
Instead of what i want to achieve the code just increases the value of the first field of the array.
It looks like the while loop just ignores it's own condition but the values itself don't seem to be the problem.
I don't really get why the while loop behaves like this.
Do i completly miss something here?
Would appreciate any help.
Greetings Sleepy
The problem is that you're storing the values as strings, not numbers, and you're using the === operator, which doesn't perform type coercion between different types. count($pw_signs) - 1 will always be a number, not a string, all the === tests will fail.
Get rid of the quotes around all the numbers and it should work as desired. And if the source of the values is external, convert them to numbers with intval() before storing into the array.
Related
i read information out of a MSSQL database and save this into arrays with a length of 2000.
When i echo the entries i get the length of these arrays in the first entry (e.g. amount[0] = 2000).
Does php do this by default or is there no default value?
(The cells in my database which go into these arrays are empty in the first entry)
Thank You
If i understand your question very well. PHP uses a function called count() to determine the length of an array ,you can do somethiing like this,
$x = [12,11,334,56,788,999,45,56,23];
$y = count($x);
then change the value of the first array element like this,
$x[0] = $y;
So whenever you call $x[0] you will get the length of the array.
I'm trying to make a POST method that will receive a value from a table (that is dynamically generated). This value will be equal to a company name, and a hidden field will be there that is equal to company name + "id" appended to it.
Here's my code:
if(isset($_POST))
{
foreach ( $users as $balance_user ) {
if(isset($_POST[$balance_user]))
{
//update user meta with new balance
$newBalance = $_POST[$balance_user];
$postedID = $_POST[$balance_user.'id'];
update_user_meta($postedID, 'balance', $newBalance);
}
}
}
I keep getting the error Illegal offset type in isset or empty. Can I not pass variables in that way? For example if a company is called Acme, and that particularly named input has a value in it, I want to loop through all of the companies in the POST method, and if that part of the loop equals the company passed in the variable, it should do something.
Add these three lines to see the data, as others have indicated, clearly you are assuming some value is in $balance_user which is not there, or is different.
echo '<pre>:';
var_dump($balance_user);
echo ':</pre>';
if(isset($_POST[$balance_user]))
The pre makes it easier to read the debugging output. the :..: will show null values.
Once you run that, you will probably discover that one of your entries in $users is empty.
The output order will show you where that empty user value is.
However:
$postedID = $_POST[$balance_user.'id'];
That could be the error source as well, is there a post value that is. say, $balance_user == fred
fredid
if there isn't, of course you will instantly get that error. You aren't giving the line number of the error so I can't tell which it is, the line number will show it instantly.
I'm trying to count a php array.
I have my code successfully counting it, but the value is returning one digit higher than what my array is.
I have tried using -- when echoing my array, but that doesn't work.
Here is my code so far:
$quotes[0] = "Volvo";
$quotes[1] = "BMW";
$quotes[2] = "Toyota";
$quotesCount = count($quotes);
echo ($quotes[rand(0, 2)]);
echo $quotesCount--;
When it count's it returns "3" which makes sense because there are three items, but how do I subtract a number when it echos so that it reflects the the largest digit in the array?
What you tried with the echo $quotesCount--; is almost doing what you want it to. What you missed though is how the -- works. You can place it either infront of the variable or behind it - and that makes a difference.
To get the full version, read this: http://php.net/manual/en/language.operators.increment.php
But the short version is that you could potentially do this:
echo --$quotesCount;
Which will show you the value you want.
However this is still not really true - you are confusing array keys with the count of elements in an array.
If your array had non-sequential keys (1,3,5) for example, that code would return 2 - which is certainly not the highest key.
You can get a nice stepping stone to the key itself by using http://php.net/manual/en/function.array-keys.php - then you can reference the actual key itself by its order in the array.
You can use array_max($quotes) z this will return the highest key in the array.
Hey" you should array_max($array) in this case.
array_max is an array function which returns the highest value of an array.
That's it,
Keep Coding :)
I've got a method that returns a string with predefined length of random alpha numbers.
I just let it run until it comes across a duplicate and breaks out of the loop and display the amount of generations it took before a duplicate was generated.
The loop numbers will be in the millions, and I am wondering if this really is good and efficient of testing it?
You can use array_search
The function return the string and store it to an array.
Then use array_search to see if it is not FALSE and loop.
Put The generated strings into an array as a key before outputting them.
$array[$alpha_string] = (isset($array[$alpha_string])) ? $array[$alpha_string] + 1 : 1;
Then check which items have a count > 1.
I can't explain this. I have the following:
$time += $res['timezone']; (The array equates to -5*3600 (EST))
return gmstrftime('%c',$time);
When I echo $res['timezone'], I get "-5*3600" which is correct. When I put the array value in front of the time variable, I get the incorrect time. If I comment out the array value and replace it with -5*3600, I get the correct result. Why??
because the string "-5*3600" and the expression -5*3600 aren't the same thing. You could try to put eval around the array value, like so:
$time += eval($res['timezone']); //(The array equates to -5*3600 (EST))
return gmstrftime('%c',$time);
Note that this is a very bad idea, as it is both slow and insecure. If you want to store -5*3600 in the array, then calculate the value and store the result in the array:
$res['timezone'] = -5*3600;