Friday, February 06, 2015

Arrays in PHP - 3


Let's try some more stuffs using PHP Arrays. Check the previous Array articles  Arrays in PHP - 2, Arrays in PHP

1. Problem :: How to get the last element in an array without using a loop.
Solution :: We need to use key() and end() functions in order to achieve this. Check out the code below.
   
   <?php
   // Define the Array
   $arr =  array( 's1' => "100", 's2' => 300, 's3' => 100, 's4' => 200, 's5' => 300 );
   
   // moves the array pointer to the end 
   // of the Array and return the value
   $l = end($arr);
   // Get that element's key
   $k = key($arr);
   // Print Key and Value
   echo "<br>$k :: $l";
   
   // Bring back the array pointer to the
   // beginning of the array
   reset($arr);
   
   // Get the Current item's key
   $k = key($arr);
   // Get the Value
   $l = current($arr);
   echo "<br>$k :: $l";
   // Print Key and Value
   echo "<br>$k :: $l";
 ?>   
   
   
2. Problem :: We have the following array and find all the keys where the value is 100.
   $arr =  array( 's1' => "100", 's2' => 300, 's3' => 100, 's4' => 200, 's5' => 300 );
   
  Solution :: We'll be using array_search() function which returns keys of the array. If we pass only the array name as the first parameter, all the keys are returned as array. However, if we give any value as second parameter, that value will be searched within the array and if found all respective keys are returned as array.
   
 <?php
   // Define the Array
   $arr = { 's1' => "100", 's2' => 300, 's3' => 100, 's4' => 200, 's5' => 300};
   
   // Print all Keys wihtin the array
   print_r( array_keys( $arr ) );
   
   // Print all keys where value is 100
   print_r( array_keys( $arr, 100 ) );
?>
   
   As PHP values can be implicitly typecast to other types (like integer to string), a string value "100" will be considered as a match while array_key() searches for integer 100.

3. Problem :: Passing Arrays to a function using call-by-reference.
   Solution :: As of PHP 5.3, passing arrays as reference to function is deprecated. To force it, we need to set 'allow_call_time_pass_reference' to true in php.ini.
   So, in general Arrays are passed by values to functions. Hence any changes made to the passed array within the called function does not make alterations to the original array. Here is a small example :
   
   <?php
   
   // Define the Array
   $arr =  array( "100", 300, 100, 200, 300 );
   
   // Define Function
   $f = create_function( '$a', '
for($i=0; $i<count($a);$i++)
 $a[$i]++;
   ');
   
   // Call Function
   $f($arr);
   
   // Print the Array
   echo '<br>After function call :: ';
   print_r ($arr);
 ?>
   
   The output of the above program will be same as the original array declared at the beginning of the program.

4. Strings can be treated as an array of characters. The same behaviour can be seen in C, C++ and Python. Check the code below ..
   
  <?php
   // Sample String
   $str = "This is a Balloon";
   
   // Start iterating with the  
   // index starting at zero
   for($i=0;$i<strlen($str);$i++)
     echo $str[$i];
 ?>

   The same is applicable to array of strings which can be printed character by character... just the way we do it in case of multi-dimensional arrays.  Check the example below :
   
  <?php
   // Declare array of Strings
   $arr = array( 
                "Jackie",
"Johnny",
"Varun",
"Catherine"
       ) ;
   // For loop to track strings
   for($i=0;$i<count($arr);$i++)
   { 
      echo "<br>";   
 
      // Now loop through characters in each strings
      for($j=0;$j<strlen($arr[$i]);$j++)
      {  
// Access each character as 
// a multi-dimensional arrays
echo $arr[$i][$j];
      }
   }
 ?>