Check for negative values in an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Check for negative values in an array

Post 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 :)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

Number 3 please ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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');
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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 :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

array_filter() could also be used, with a simple callback function like jenk provided.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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).
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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 :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're all over complicted. :P

Code: Select all

$hasNegatives = min($array) < 0;
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

Thank you both of you :)
Post Reply