Difference between count() and sizeof() in PHP

difference between count and sizeof

According to PHP documentation, sizeof() is an alias of  count(). While searching on the web, I found and realized that count() is faster and butter than sizeof(). Here is a good description of this on stackoverflow– scroll down to the answer of Gazoris. to understand the difference between count() and sizeof() in php

According to my understanding when sizeof is an alias of count, its code might be looking something like this.

count() Function:

  1. function count($array_variable){
  2. //...........
  3. //code for counting size of array
  4. //...........
  5. return $size;
  6. }

sizeof() Function:

  1. function sizeof($array_variable){
  2. return count($array_variable);
  3. }

The above example code describes that when you are using sizeof() you are also calling the count() via backdoor. So, use of count is better.

Store all data in an array to remove duplicate record when using LEFT JOIN in MySql

Sometime you’d stuck like that you’ll need all other rows coming with left join as a duplicate row for all record, where you would need to have all these inside an array variable. For example if you have an tbl_emp and another tbl_contact, in tbl_contact you would have one foreign key emp_id.

You made a query to fetch all employee record with their contact nos, when you left join the query such like LEFT JOIN tbl_contact ON tbl_contact.emp_id=tbl_emp.emp_id, you will get record in associative array something like this.

array (size=3)
  0 => 
    array (size=3)
      'name' => string 'test1' (length=5)
      'age' => int 23
      'email' => string 'Good' (length=4)
  1 => 
    array (size=3)
      'name' => string 'test2' (length=5)
      'age' => int 24
      'email' => string 'Good' (length=4)
  2 => 
    array (size=3)
      'name' => string 'test2' (length=5)
      'age' => int 24
      'email' => string 'Best' (length=4)

 Here you are getting two record for test2 employee, because this employee has two email as a contact.

So, to simplify this you will need to have all contact nos inside a single variable key and the duplicate record to be removed, then here is the logic to do that.

 

 

  1. <?php
  2. $total_size=sizeof($arr);
  3. for($i=0;$i<$total_size;$i++){
  4. $arr[$i]['emails']=array();
  5. $arr[$i]['emails'][]=$arr[$i]['email'];
  6. $unset=0;
  7. foreach($arr[$i] as $key=>$value){
  8. if($key=='name' && $arr[$i]['name']==$arr[$i+1]['name']){
  9. $arr[$i]['emails'][]=$arr[$i+1]['email'];
  10. $unset=1;
  11. }
  12. }
  13. unset($arr[$i]['email']);
  14. if($unset==1){
  15. unset($arr[$i+1]);
  16. $i++;
  17. }
  18. }
  19. var_dump($arr);
  20. ?>

 

 

Now you’ll get the result something like that

array (size=2)
  0 => 
    array (size=3)
      'name' => string 'test1' (length=5)
      'age' => int 23
      'emails' => 
        array (size=1)
          0 => string 'Good' (length=4)
  1 => 
    array (size=3)
      'name' => string 'test2' (length=5)
      'age' => int 24
      'emails' => 
        array (size=2)
          0 => string 'Good' (length=4)
          1 => string 'Best' (length=4)

 

 

Array Sorting With Single Loop

There is an unsorted array of integers. write the complete program to find the second largest no in the array without using sorting and max functions. You can only iterate(loop) over the array once.

 

 

  1. <?php
  2. echo "<br/>";
  3. $arr=array(12,52,2,35,95,17,37,42);
  4. echo "Before Sorting<br/>";
  5. print_r($arr);
  6. $tot_arr=count($arr);
  7. for($i=0;$i<$tot_arr;$i++){
  8. if(($i< ($tot_arr-1)) && $arr[$i]>$arr[$i+1]){
  9. $temp=$arr[$i];
  10. $arr[$i]=$arr[$i+1];
  11. $arr[$i+1]=$temp;
  12. $i=-1;
  13. }
  14. }
  15. echo "<br/>After Sorting<br/>";
  16. print_r($arr);
  17. ?>

 

 

Output:

 

Before Sorting
Array ( [0] => 12 [1] => 52 [2] => 2 [3] => 35 [4] => 95 [5] => 17 [6] => 37 [7] => 42 ) 
After Sorting
Array ( [0] => 2 [1] => 12 [2] => 17 [3] => 35 [4] => 37 [5] => 42 [6] => 52 [7] => 95 )