implementing count method in php

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 menukadevinda  Icon User is offline

  • D.I.C Regular

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

implementing count method in php

Posted 06 July 2012 - 11:46 PM

Hi all,

I just implemented count function in php that return number of elements in a array.

 function mycount($arr)
{
       $i=0;
       while(!empty($arr[$i])){
           
           $coun=$i;
           $i++;
           
       }
        return $coun+1;
}



now we can use this methode to get number of elements in array. If you think there is any problem or issue let me know.

thanks in advance..

Is This A Good Question/Topic? 0
  • +

Replies To: implementing count method in php

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: implementing count method in php

Posted 07 July 2012 - 12:15 AM

I see two issues with your function:

  • You are duplicating the counter variable. You already have $i, which represents the count. Why do you copy it over to $coun? You may as well just remove $coun altogether and return $i.

  • Using empty() to determine if an element is set skews your results, if your intent is really to return the number of set elements in an array. It will exclude elements set to values like the number 0, FALSE, empty arrays, empty strings, and of course NULL. An element set to any of these (except perhaps NULL, one might argue) should be considered a part of the count. - You'd do better to use isset()


But this function is ultimately unnecessary, as I am sure you already know, seeing as PHP already has a count() function built in. Unless of course your goal is to do some sort of manual filtering of what is counted.
Was This Post Helpful? 3
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: implementing count method in php

Posted 07 July 2012 - 02:37 AM

View PostAtli, on 07 July 2012 - 09:15 AM, said:

Unless of course your goal is to do some sort of manual filtering of what is counted.

and in that case you would filter the array and count() the resulting array.
Was This Post Helpful? 3
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: implementing count method in php

Posted 07 July 2012 - 02:45 AM

Quote

Posts: 435
Joined: 14-April 11


/facepalm
Was This Post Helpful? 4
  • +
  • -

#5 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 07 July 2012 - 08:08 PM

thanks atli,

I could figure out weak points of my programme. yes u r correct it is usless to overide count function but I intended to learn something from my weak points.
 function mycount($arr)
{
       $i=0;
       while(!empty($arr[$i])){
           
           $coun=$i;
           $i++;
           
       }
        return $i;
}




thanks you guys and I am too thinking of JackOfAllTrades post.

hi ,

 function mycount($arr)
{
       $i=0;
       while(!empty($arr[$i])){
           
        
           $i++;
           
       }
        return $i;
}





corrected one should like this be and I notice my editing previlage also remove you guys :(
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: implementing count method in php

Posted 07 July 2012 - 08:26 PM

Yes, you upvoted me for removing your editing privileges here. You removed your code b/c it was a school assignment, which violates the rules here.
Was This Post Helpful? 0
  • +
  • -

#7 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 07 July 2012 - 08:33 PM

ok. I am sorry about that and can I get my rights to edit back?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,092
  • Joined: 08-August 08

Re: implementing count method in php

Posted 07 July 2012 - 08:35 PM

If you're trying to learn something, start with proper indenting:

function mycount($arr)
{
	$i=0;
	while(!empty($arr[$i])){           
		$i++;
	}
	return $i;
}


Also note that this function will not always return the actual size of the array, but merely the first empty index. For example, this returns 2:
<?php
$test = array();

$test[0] = 100;
$test[1] = 200;
$test[3] = 400;
$test[4] = 500;

echo mycount($test);

function mycount($arr)
{
	$i=0;
	while(!empty($arr[$i])){           
		$i++;
	}
	return $i;
}
?>

Was This Post Helpful? 3
  • +
  • -

#9 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 07 July 2012 - 09:40 PM

Hi CTphpnwb,

it is amazing can you explain how it out puts 2? it should be 4.

and as Atli mention to use isset() methode instead of empty() , out put 0.
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,092
  • Joined: 08-August 08

Re: implementing count method in php

Posted 07 July 2012 - 11:59 PM

Does $test[2] contain a value? What will happen when $i is 2?
Was This Post Helpful? 1
  • +
  • -

#11 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 08 July 2012 - 12:11 AM

ahhh,though I knew what you was talking,I didnt notice $test[2] was empty.

thanks..
Was This Post Helpful? 0
  • +
  • -

#12 bluman  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 05-January 10

Re: implementing count method in php

Posted 08 July 2012 - 01:27 AM

A much easier way to do this is using the foreach structure. I'm not saying it's optimal. Also there is the count function already implemented in the standard library so this is kind of pointless.

function mycount($array)
{
	$count = 0;
	foreach ($array as $record)
		$count++;
	return $count;
}


Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: implementing count method in php

Posted 08 July 2012 - 02:06 AM

that is not even doing any filtering, i.e. it is the same as count()
Was This Post Helpful? 1
  • +
  • -

#14 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 08 July 2012 - 09:16 PM

HI,

this will out put the correct result.
finally, this works fine and I could grasp clearly empty() and isset() method in php.
<?php
        //function to count number of elemenst in a array
        function mee($arr){
       $i=0;
       while(isset($arr[$i])){
           
        
           $i++;
           
       }
        return $i;
       }
       
       
$test = array(100,200,300,400);

$test[0] = 100;
$test[1] = 200;
$test[3] = 400;
$test[4] = 500;

echo mee($test);
    
   
?> 





Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,092
  • Joined: 08-August 08

Re: implementing count method in php

Posted 09 July 2012 - 05:59 AM

It may work, but you've failed to grasp the concept of proper indenting. It's important, so learn it.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2