array_count_values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    array_count_values

    hello,

    here is the code:

    <?
    $a=array(1,3,4, 5,6,7,8,9,10);
    $b=array_count_ values($a);
    print_r($b);
    ?>

    the result will be: [1]=>1 [3]=>1...........

    I would like that the keys which doesnt appear in the result of
    array_count_val ues (here it is the number 2)
    have the value 0.......the new result will be: [1]=>1 [2]=>0
    [3]=>1...........

    how can we formulate a php code to resolve that ?


  • Pedro Graca

    #2
    Re: array_count_val ues

    <a> wrote:[color=blue]
    > I would like that the keys which doesnt appear in the result of
    > array_count_val ues (here it is the number 2)
    > have the value 0.......the new result will be: [1]=>1 [2]=>0
    > [3]=>1...........
    >
    > how can we formulate a php code to resolve that ?[/color]


    php$ cat countall.php
    <?php
    $a = array(1, 3, 4, 5, 6, 7, 8, 9, 10);
    $b = array_count_val ues($a);

    #print_r($b);

    for ($i = 0; $i <= max($a); ++$i) {
    if (!isset($b[$i])) $b[$i] = 0;
    }
    ksort($b);
    print_r($b);
    ?>

    php$ php countall.php
    Array
    (
    [0] => 0
    [1] => 1
    [2] => 0
    [3] => 1
    [4] => 1
    [5] => 1
    [6] => 1
    [7] => 1
    [8] => 1
    [9] => 1
    [10] => 1
    )

    --
    Mail to my "From:" address is readable by all at http://www.dodgeit.com/
    == ** ## !! ------------------------------------------------ !! ## ** ==
    TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
    may bypass my spam filter. If it does, I may reply from another address!

    Comment

    • Jan Pieter Kunst

      #3
      Re: array_count_val ues

      a wrote:[color=blue]
      > hello,
      >
      > here is the code:
      >
      > <?
      > $a=array(1,3,4, 5,6,7,8,9,10);
      > $b=array_count_ values($a);
      > print_r($b);
      > ?>
      >
      > the result will be: [1]=>1 [3]=>1...........
      >
      > I would like that the keys which doesnt appear in the result of
      > array_count_val ues (here it is the number 2)
      > have the value 0.......the new result will be: [1]=>1 [2]=>0
      > [3]=>1...........
      >
      > how can we formulate a php code to resolve that ?[/color]

      Like this, for example:

      $a = array(1,3,4,5,6 ,7,8,9,10);
      $range = range(1,10);
      foreach($range as $number) {
      if (in_array($numb er, $a)) {
      $b[$number] = 1;
      } else {
      $b[$number] = 0;
      }
      }

      JP

      --
      Sorry, <devnull@cauce. org> is een "spam trap".
      E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

      Comment

      • Pedro Graca

        #4
        Re: array_count_val ues

        Pedro Graca wrote:[color=blue]
        > <?php
        > $a = array(1, 3, 4, 5, 6, 7, 8, 9, 10);
        > $b = array_count_val ues($a);
        >
        > #print_r($b);
        >
        > for ($i = 0; $i <= max($a); ++$i) {[/color]

        Make that

        for ($i = 1; $i < max($a); ++$i) {
        [color=blue]
        > if (!isset($b[$i])) $b[$i] = 0;
        > }
        > ksort($b);
        > print_r($b);
        > ?>[/color]

        --
        Mail to my "From:" address is readable by all at http://www.dodgeit.com/
        == ** ## !! ------------------------------------------------ !! ## ** ==
        TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
        may bypass my spam filter. If it does, I may reply from another address!

        Comment

        • scotty

          #5
          Re: array_count_val ues

          I think the below works.

          Scott


          $a=array(1,3,4, 5,6,7,8,9,10);
          $b=array_count_ values($a);

          //sort the key
          ksort($b);

          //document highest key
          foreach($b as $key=>$value)
          {
          $counter=$key;
          }

          //load elements that do not exist with zeros
          for($i=$counter ;$i>=0;$i--)
          {
          if(!isset($b[$i]))
          {
          $b[$i]=0;
          }
          }

          //sort by key again
          ksort($b);
          print_r($b);






          <a> wrote:[color=blue]
          > hello,
          >
          > here is the code:
          >
          > <?
          > $a=array(1,3,4, 5,6,7,8,9,10);
          > $b=array_count_ values($a);
          > print_r($b);
          > ?>
          >
          > the result will be: [1]=>1 [3]=>1...........
          >
          > I would like that the keys which doesnt appear in the result of
          > array_count_val ues (here it is the number 2)
          > have the value 0.......the new result will be: [1]=>1 [2]=>0
          > [3]=>1...........
          >
          > how can we formulate a php code to resolve that ?[/color]

          Comment

          • scotty

            #6
            Re: array_count_val ues

            Pedro's is more concise.

            Scotty

            Comment

            • scotty

              #7
              Re: array_count_val ues

              Pedro's is more concise.

              Scotty

              Comment

              • Janwillem Borleffs

                #8
                Re: array_count_val ues

                a wrote:[color=blue]
                > I would like that the keys which doesnt appear in the result of
                > array_count_val ues (here it is the number 2)
                > have the value 0.......the new result will be: [1]=>1 [2]=>0
                > [3]=>1...........
                >
                > how can we formulate a php code to resolve that ?[/color]

                <?php

                $a = array(1,3,4,5,6 ,7,8,9,10);
                $b = array_count_val ues($a);
                $c = array_fill(1,10 ,0);
                $d = array_diff($c,$ b);
                ksort($e = $b + $d);
                print_r($e);

                ?>


                JW



                Comment

                • Guest's Avatar

                  #9
                  Re: array_count_val ues

                  Tank you for all these answers !


                  Comment

                  Working...