implementing count method in php

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1499 Views - Last Post: 10 July 2012 - 04:15 AM Rate Topic: -----

#16 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: implementing count method in php

Posted 09 July 2012 - 07:13 AM

actually this is not the firstime I was redirect to this external link. :)
This time I am going to correct myself in indeting. Thanks CTphpnwb to note me that again.this time I take this is serious.;)
Was This Post Helpful? 0
  • +
  • -

#17 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: implementing count method in php

Posted 09 July 2012 - 11:57 AM

It should also be noted that your function will not return the proper count if there are gaps in the indexes:
This:
<?php
        //function to count number of elemenst in a array
        function mee($arr){
			$i=0;
			while(isset($arr[$i])){
				$i++;
			}
			return $i;
       }
       
$test = array();
$test[0] = "This";
$test[1] = "is";
$test[4] = "a";
$test[5] = "test";

echo mee($test);
    
?> 



Will incorrectly return a value of 2.
Was This Post Helpful? 0
  • +
  • -

#18 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3029
  • View blog
  • Posts: 10,551
  • Joined: 08-August 08

Re: implementing count method in php

Posted 09 July 2012 - 04:07 PM

View PostJstall, on 09 July 2012 - 02:57 PM, said:

It should also be noted that your function will not return the proper count if there are gaps in the indexes:

:smartass: Beat you to it! Post #8
Was This Post Helpful? 1
  • +
  • -

#19 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: implementing count method in php

Posted 10 July 2012 - 04:15 AM

Haha so you did, I somehow missed that :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2