exclusive value in array [php]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naughty
    Confirmed User
    • Jul 2001
    • 6487

    #1

    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?
    seks.ai for sale - ping me
  • Alky
    Confirmed User
    • Apr 2002
    • 5651

    #2
    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;
    }
    }

    Comment

    • Alky
      Confirmed User
      • Apr 2002
      • 5651

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

      Comment

      • Varius
        Confirmed User
        • Jun 2004
        • 6890

        #4
        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.
        Skype variuscr - Email varius AT gmail

        Comment

        • Naughty
          Confirmed User
          • Jul 2001
          • 6487

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

          But this does not ISOLATE 100, it just looks if it there.
          seks.ai for sale - ping me

          Comment

          • Naughty
            Confirmed User
            • Jul 2001
            • 6487

            #6
            @Varius: I'll check that. At first glance, what if the array has 2x100. Like this:
            $a = array(100,200,100,400);
            seks.ai for sale - ping me

            Comment

            • Alky
              Confirmed User
              • Apr 2002
              • 5651

              #7
              Originally posted by Varius
              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.

              Comment

              • Naughty
                Confirmed User
                • Jul 2001
                • 6487

                #8
                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);
                seks.ai for sale - ping me

                Comment

                • Alky
                  Confirmed User
                  • Apr 2002
                  • 5651

                  #9
                  Originally posted by Naughty
                  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.

                  Comment

                  • Naughty
                    Confirmed User
                    • Jul 2001
                    • 6487

                    #10
                    Originally posted by Alky
                    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.
                    seks.ai for sale - ping me

                    Comment

                    • Naughty
                      Confirmed User
                      • Jul 2001
                      • 6487

                      #11
                      i'm a genius, i'm on to something ...
                      seks.ai for sale - ping me

                      Comment

                      • gornyhuy
                        Chafed.
                        • May 2002
                        • 18041

                        #12
                        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.

                        icq:159548293

                        Comment

                        • Naughty
                          Confirmed User
                          • Jul 2001
                          • 6487

                          #13
                          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";
                          
                          }
                          seks.ai for sale - ping me

                          Comment

                          • Naughty
                            Confirmed User
                            • Jul 2001
                            • 6487

                            #14
                            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";
                            } 
                            
                            seks.ai for sale - ping me

                            Comment

                            Working...