GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   exclusive value in array [php] (https://gfy.com/showthread.php?t=870960)

Naughty 11-22-2008 02:58 PM

exclusive value in array [php]
 
I'm fucking with this script and i need toi get this simple thing done.

a. array(100,200,400);
b. array(100);
c. array(200);

100 is my goal here. I need to know what array has ONLY 100 in there (b.).


Any kids drinking at home tonight?

Alky 11-22-2008 03:09 PM

probably a better way to do it then i did it...and i didnt test it
Code:

fuction chkArr($array) {
foreach($array as $k=>$v) {
if($v!=100) {
return False;
} else {
$didfind=True;
}
}
if($didfind==True) {
return true;
} else {
return false;
}
}


Alky 11-22-2008 03:17 PM

yea you can you in_array() in place of some of my code above

Varius 11-22-2008 03:34 PM

This should have you on your way :)

Code:

$array = array(array(100,200,400), array(100), array(200));

foreach ($array as $array2) {
        if ($array2[0]==100 && count($array2)==1) {
                $arrMatches[] = $array2;
        }
}

print_r($arrMatches);

I made an array there out of the arrays that ONLY contained the value 100, but you could just echo out the keys instead or whatever ya like.

Naughty 11-22-2008 03:34 PM

Nope, i have that
if (in_array(200,$p2osArr)){print "okay ja -> ";}

But this does not ISOLATE 100, it just looks if it there.

Naughty 11-22-2008 03:36 PM

@Varius: I'll check that. At first glance, what if the array has 2x100. Like this:
$a = array(100,200,100,400);

Alky 11-22-2008 03:38 PM

Quote:

Originally Posted by Varius (Post 15092011)
This should have you on your way :)

Code:

$array = array(array(100,200,400), array(100), array(200));

foreach ($array as $array2) {
        if ($array2[0]==100 && count($array2)==1) {
                $arrMatches[] = $array2;
        }
}

print_r($arrMatches);

I made an array there out of the arrays that ONLY contained the value 100, but you could just echo out the keys instead or whatever ya like.

not sure if he will run into the problem, but if the array has 100 twice it fails.

Naughty 11-22-2008 03:38 PM

Oh, and there's one array, i should have written this:
$a = array(100,200,400);
$a = array(100,200,100,400);
$a = array(100);
$a = array(200);

Alky 11-22-2008 03:40 PM

Quote:

Originally Posted by Naughty (Post 15092023)
Oh, and there's one array, i should have written this:
$a = array(100,200,400);
$a = array(100,200,100,400);
$a = array(100);
$a = array(200);

uhm, thats impossible if you do it after those 4 lines...you are making a new array each line.

so the array only has 200 in it.

Naughty 11-22-2008 03:51 PM

Quote:

Originally Posted by Alky (Post 15092035)
uhm, thats impossible if you do it after those 4 lines...you are making a new array each line.

so the array only has 200 in it.

That was an example of how the arrays would like when being checked. I thought that was obvious, sorry.

Naughty 11-22-2008 03:54 PM

i'm a genius, i'm on to something ...

gornyhuy 11-22-2008 03:57 PM

Varius got it right. It doesn't fail on multiple 100's. You are asking if it has only one value and if that value is 100. That is what he is checking for.

His first condition checks for value of 100. His second condition checks if there is more than one value. Done.

Naughty 11-22-2008 03:59 PM

THIS does exactly what i want:
Code:

$a = array(100,100,100);
//$a = array(200,100,100);
if (in_array(100,$a)){
$tTnt_cnt = array_sum($a);
$tItems_cnt = count($a);

print (($tTnt_cnt/$tItems_cnt) > 100)?"parcel":"envelope";

}


Naughty 11-22-2008 04:07 PM

Even better, now when they have 4+ items, they have to get parcelpackaging anyhow;-)
*pads self on the back

PHP Code:

$a = array(100,100,100);
//$a = array(200,100,100);
if (in_array(100,$a)){
$tTnt_cnt array_sum($a);
$tItems_cnt count($a);
print ((
$tTnt_cnt/$tItems_cnt) == 100 && $tItems_cnt<=3)?"envelope":"parcel";




All times are GMT -7. The time now is 07:12 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123