Wednesday, December 29, 2010

PHP array functions

today myblog

date 29/12/2010

today i learning array functions in php


array_change_key_case
-----------------------

<?php


$x=array('name'=>'john','age'=>25,'city'=>'mumbai');


print_r(array_change_key_case($x,CASE_UPPER));


?>

the output for the above programme is ---->


Array ( [NAME] => john [AGE] => 25 [CITY] => mumbai )




array_combine
----------------------
 
              this array function will taking this arguments as first array treating as array keys and the second array treated as values
                 so automatically the output will get an associative array


<?php


$a=array('name','age','city');

$b=array('john',25,'chennai');


$c=array_combine($a,$b);

print_r($c);

?>


the output for the above code is
Array ( [name] => john [age] => 25 [city] => chennai )



array_merge
------------


 this function will combine and merge the two arrays

<?php


$x=array('name'=>'john','city'=>'chennai');

$y=array('age'=>25,'class'=>'students');


$z=array_merge($x,$y);


print_r($z);

?>

output for the above code is

Array ( [name] => john [city] => chennai [age] => 25 [class] => students )


array_count_values
--------------------

this function returns the same values how many times appeared in that array

<?php


$x=array('city','chennai','welcome','resource','gandhi','freedom','chennai','welcome','chennai','resource','city');


print_r(array_count_values($x));


?>

output for the above program is

Array ( [city] => 2 [chennai] => 3 [welcome] => 2 [resource] => 2 [gandhi] => 1 [freedom] => 1 )


array_diff_assoc
-------------------

      this array function comparises two arrays and it eliminates the same values in both array and prints the odd values

    <?php


$x=array('chennai','city','welcome','source');

$y=array('chennai','city','welcome');

$z=array_diff_assoc($x,$y);

print_r($z);

?>

output for the above programme is

                    Array ( [3] => source )


array_diff_key
-----------------

                    in this array function it will intersect the both array keys and eliminates them and print the odd keys  first argument array key values



<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

print_r(array_diff_key($array2, $array1));

?>

o/p

                Array ( [yellow] => 7 [cyan] => 8 )


array_fill_key
---------------

                this function will fills the key values to the array keys

                      <?php

                $keys=array('name','school','welcome');
               
                $y=array_fill_keys($keys,'oniorns');

                print_r($y);

                       ?>
output:-
--------------

                       Array ( [name] => onions [school] => onions [welcome] => onions )



array_fill
--------------

               this function fills insert values into the arrays

array_flip
-------------

              this function returns the values as keys and keys as values  funny interesting thing to change  the keys and values

          <?php



$x=array('name'=>'kiran','age'=>25,'city'=>'chennai');


$y=array_flip($x);

print_r($y);

?>

output:--
------------

           Array ( [kiran] => name [25] => age [chennai] => city )



array_intersect_assoc
----------------------

              this function intersects both the arrays and it will print the common elements in both the arrays

              <?php


         $x=array('x'=>45,'y'=>25,'z'=>65);

         $y=array('x'=>42,'z'=>65);

         $z=array_intersect_assoc($x,$y,);

         print_r($z);

         ?>

output of the code

              Array ( [z] => 65 )

array_intersect_key
------------------------

<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

print_r(array_intersect_key($array1, $array2));
?>


output for the code is
---------------------------

         Array ( [blue] => 1 [green] => 3 )


array_intersect
            this function returns the intersection of both the arrays


array_key_exists
----------------
                this code will check whether the key exists or not in the programme

     <?php

$x=array('name'=>'kiran','city'=>'chennai');

if(array_key_exists('city',$x))
{
     echo "find in the array";
   
 }
 else
   echo "not found in the array";
  
   ?>

    output :=  find in the array

array_keys
------------
             it will print the array keys

array_map
-------------

                <?php


function scope($x)

{
   
    return($x*$x*$x);
   
}


$a=array(4,5,8,9);

$b=array_map("scope",$a);

print_r($b);

?>

the above code represents the array_map it getting the value from the array and pass it to another array for calucalting the function

output is
    Array ( [0] => 64 [1] => 125 [2] => 512 [3] => 729 )

1 comment:

  1. Nice and very useful .

    Keep posting

    Regards

    sathia

    ReplyDelete