Page 1 of 1
Check for negative values in an array
Posted: Wed Aug 30, 2006 5:46 am
by Dave2000
As part of a script i am making, i wish to do a check to ensure that an array contains no negative values. How could i do this please?
Thank you
Shears

Posted: Wed Aug 30, 2006 5:49 am
by Oren
It depends, what would you like to do?
1. Get rid of the negative values.
2. Replace them with some default value.
3. Generate an error when one or more elements of the array conain negative values.
Posted: Wed Aug 30, 2006 5:53 am
by Dave2000
Number 3 please

Posted: Wed Aug 30, 2006 6:12 am
by Jenk
Code: Select all
function checkValue (&$val, $key)
{
if ($val < 0) {
trigger_error('Value must be positive for ' . $key, E_USER_WARNING);
}
}
array_walk($array, 'checkValue');
Posted: Wed Aug 30, 2006 6:24 am
by Dave2000
Thank you. That was what i wanted. I have one question. Could you tell me what the '&' sign before '$val' is for?
Thank you
Shears

Posted: Wed Aug 30, 2006 7:43 am
by Jenk
It's to indicate that the value of $val is to be
passed by reference
However, it is is not essential in that scenario

Posted: Wed Aug 30, 2006 8:02 am
by s.dot
array_filter() could also be used, with a simple callback function like jenk provided.
Posted: Wed Aug 30, 2006 8:58 am
by Oren
scottayy wrote:array_filter() could also be used, with a simple callback function like jenk provided.
While array_filter() might work too, it's not logical to use it here - it's not array_filter()'s purpose.
array_filter() is good for #1 and #2 from my previous post though (see above).
Posted: Wed Aug 30, 2006 11:27 am
by Dave2000
Well it appeared to work with a trial array i wrote out, but as of yet i still i haven't sucessfully intergrated it into my script.
Some background. I am making a game and making an (armoury) page to buy and sell items for the game. This thread is to do with the selling section on the page. Essentially, i would like the page to read, if number of items is negative, ouput "Not enough items in armoury for sale to take place" else, update database with new numbers for each item.
Code: Select all
function checkValue (&$val)
{
if ($val >= 0) {Echo 'Update ';
} elseif ($val < 0) {Echo 'Not enough weapons in armoury for sale to take place.</p>';
}
}
array_walk($numitems, 'checkValue');
I thought maybe the above would work, and i have tried several similar variations. (For simplicity, in the code above, i am just using the word "update" where the 'update sql' statement will go.)
On testing. If array $numitems contains no negative value, 'Update' is echoed to the screen 6 times - i only need it once. If array contains one or more negative values, 'Update' is echoed to the browser 6 times AND 'Not enough weapons in armoury for sale.' is echoed to the browser.
I am wondering if anyone has any ideas for how i could solve my problem.
Thank you
Shears

Posted: Wed Aug 30, 2006 12:41 pm
by Jenk
Code: Select all
foreach ($array as $val) {
if ($val < 0) {
echo 'Not enough weapons in armory for sale';
$var = null;
break;
} else {
$var = 'update';
}
}
//do something with $var
Posted: Wed Aug 30, 2006 3:49 pm
by feyd
You're all over complicted.

Posted: Wed Aug 30, 2006 7:54 pm
by Dave2000
Thank you both of you
