I have an array in which I have stored a sorted set of dates, for example:

2000-12-27
2000-12-28
2000-12-28
2000-12-30
2000-12-30
2001-01-01
2001-01-01
2001-01-03
... etc

I want to build an array that hold the number of times each date occurs, fir example

2000-12-27,1
2000-12-28,2
2000-12-30,1
2001-01-01,2
2001,01-03,1

I tired using $newarray=array_count_values($myarray);

but I get the error "Warning: Can only count STRING and INTEGER values!"

So, is there a way to easily count the occurance of these dates?

Thanks for any help,
Basil

    It's probably possible with the various array functions, but here's another way.

    foreach($arr as $a)
    {
      ++$newarr[$a];
    }
    

      I may try that, but I'd like to use a PhP function like:
      $newarray = array_count_values($myarray);

      I have learned that I can not use this function with multi-dimensional arrays, so I have made "$myarray" a single dimension array with sorted dates. My question now is, how do I access individual items in the new array?

      When I do this:
      $newarray = array_count_values($myarray);
      print_r($newarray);

      this will print out all the items like this:
      Array ( [2000-12-27] => 1 [2000-12-28] => 5 [2000-12-29] => 2 [2000-12-30] => 2 [2000-12-31] => 2 [2001-01-01] => 1 [2001-01-02] => 3 ....etc.

      But when I try something like:
      echo $newarray[0][0]; to try and echo the first element, I get nothing? I;m sure its something simple, but can someone tell me, if I have user array_count_values($myarray); to make a new array that contains the occurance of each date, how do I access the data elements inside the new array?

      Basil

        I may try that, but I'd like to use a PhP function like:
        $newarray = array_count_values($myarray);

        I have learned that I can not use this function with multi-dimensional arrays, so I have made "$myarray" a single dimension array with sorted dates. My question now is, how do I access individual items in the new array?

        When I do this:
        $newarray = array_count_values($myarray);
        print_r($newarray);

        this will print out all the items like this:
        Array ( [2000-12-27] => 1 [2000-12-28] => 5 [2000-12-29] => 2 [2000-12-30] => 2 [2000-12-31] => 2 [2001-01-01] => 1 [2001-01-02] => 3 ....etc.

        But when I try something like:
        echo $newarray[0][0]; to try and echo the first element, I get nothing? I;m sure its something simple, but can someone tell me, if I have user array_count_values($myarray); to make a new array that contains the occurance of each date, how do I access the data elements inside the new array?

        Basil

          I may try that, but I'd like to use a PhP function like:
          $newarray = array_count_values($myarray);

          I have learned that I can not use this function with multi-dimensional arrays, so I have made "$myarray" a single dimension array with sorted dates. My question now is, how do I access individual items in the new array?

          When I do this:
          $newarray = array_count_values($myarray);
          print_r($newarray);

          this will print out all the items like this:
          Array ( [2000-12-27] => 1 [2000-12-28] => 5 [2000-12-29] => 2 [2000-12-30] => 2 [2000-12-31] => 2 [2001-01-01] => 1 [2001-01-02] => 3 ....etc.

          But when I try something like:
          echo $newarray[0][0]; to try and echo the first element, I get nothing? I;m sure its something simple, but can someone tell me, if I have user array_count_values($myarray); to make a new array that contains the occurance of each date, how do I access the data elements inside the new array?

          Basil

            Originally posted by Shrike
            It's probably possible with the various array functions, but here's another way.

            foreach($arr as $a)
            {
              ++$newarr[$a];
            }
            

            [/B]

            I may try that, but I'd like to use a PhP function like:
            $newarray = array_count_values($myarray);

            I have learned that I can not use this function with multi-dimensional arrays, so I have made "$myarray" a single dimension array with sorted dates. My question now is, how do I access individual items in the new array?

            When I do this:
            $newarray = array_count_values($myarray);
            print_r($newarray);

            this will print out all the items like this:
            Array ( [2000-12-27] => 1 [2000-12-28] => 5 [2000-12-29] => 2 [2000-12-30] => 2 [2000-12-31] => 2 [2001-01-01] => 1 [2001-01-02] => 3 ....etc.

            But when I try something like:
            echo $newarray[0][0]; to try and echo the first element, I get nothing? I;m sure its something simple, but can someone tell me, if I have user array_count_values($myarray); to make a new array that contains the occurance of each date, how do I access the data elements inside the new array?

            Basil

              I may try that, but I'd like to use a PhP function like:
              $newarray = array_count_values($myarray);

              I have learned that I can not use this function with multi-dimensional arrays, so I have made "$myarray" a single dimension array with sorted dates. My question now is, how do I access individual items in the new array?

              When I do this:
              $newarray = array_count_values($myarray);
              print_r($newarray);

              this will print out all the items like this:
              Array ( [2000-12-27] => 1 [2000-12-28] => 5 [2000-12-29] => 2 [2000-12-30] => 2 [2000-12-31] => 2 [2001-01-01] => 1 [2001-01-02] => 3 ....etc.

              But when I try something like:
              echo $newarray[0][0]; to try and echo the first element, I get nothing? I;m sure its something simple, but can someone tell me, if I have user array_count_values($myarray); to make a new array that contains the occurance of each date, how do I access the data elements inside the new array?

              Basil

                Write a Reply...