PHP ONLINE EDITORS || ARRAY IN PHP AND ARRAY FUNCTIONS || ITERATOR FUNCTIONS

PHP ONLINE EDITORS

PHP ONLINE EDITORS || ARRAY IN PHP AND ARRAY FUNCTIONS || ITERATOR FUNCTIONS







  • WRITE PHP ONLINE

  • PAIZA PHP EDITOR

  • PHP EDITOR

  • JDOODLE PHP EDITOR

  • PHP TESTER EDITOR

  • SANDBOX PHP EDITOR

  • REXTESTER PHP EDITOR


  • PHP FIDDLE EDITOR


ARRAY IN PHP


Index array - an array with numeric index.



?php

$name=array("amar","akbar","anthony","joker";

var_dump($name);

?>

output - array([0]=>amar,[1]=>akbar,[2]=>anthony,[3]=>joker)




Associative array - an array with string as a index and scrolls element values in association key values.




<?php

$name=array(0=>amar,1=>akbar,2=>anthony,3=>joker)

var_dump($name);

?>

output -([0]=>amar,[1]=>akbar,[2]=>anthony,[3]=>joker)

Multidimenssion array - an array contains one or more array and values.

 ARRAY FUNCTIONS


1) Sort() - this function sort an index array in asending order

    e.g  -


<?php

$animal=array("cat","dog",''tiger","lion");
$a=sort($animal);
print_r($a);
?>

output - array{a[0]=>cat,a[1]=>dog,a[2]=>lion,a[3]=>tiger}


2) rsort() - this function sort an index array in desending order.


    e.g  -


<?php

$animal=array("cat","dog",''tiger","lion");
$a=rsort($animal);
print_r($a);
?>

output - array{a[0]=>tiger,a[1]=>lion,a[2]=>dog,a[3]=>cat}


3) asort() - this function is used to sort associative array values in asending order.




    e.g  -


<?php

$animal=array("0"=>"cat","1"=>"dog","2"=>''tiger","3"=>"lion");
$a=asort($animal);
print_r($a);
?>

output - array{a[0]=>cat,a[1]=>dog,a[2]=>lion,a[3]=>tiger}


4) arsort() - this function is use for to sort associative array in desending order.


eg -


<?php

$animal=array("0"=>"cat","1"=>"dog","2"=>''tiger","3"=>"lion");
$a=arsort($animal);
print_r($a);
?>

output - array{a[2]=>tiger,a[3]=>lion,a[1]=>dog,a[0]=>cat}


5) ksort() - the ksort function sort keys arrayin asending order.


    e.g  -

  e.g  -

<?php

$animal=array("0"=>"cat","1"=>"dog","2"=>''tiger","3"=>"lion");
$a=ksort($animal);
print_r($a);
?>

output - array{a[0]=>cat,a[1]=>dog,a[2]=>lion,a[3]=>tiger}


6)krsort() - the krsort function sort the key of array in desending order.


eg -


<?php

$animal=array("0"=>"cat","1"=>"dog","2"=>''tiger","3"=>"lion");
$a=krsort($animal);
print_r($a);
?>

output - array{a[3]=>lion,a[2]=>tiger,a[1]=>dog,a[0]=>cat}


7) array_push() - this function is used to add values on to the end off  the array.


eg -


<?php

$animal=array("cat","dog",''tiger");
$a=array_push($animal,"lion");
print_r($a);
?>

output - output - array{a[0]=>cat,a[1]=>dog,a[2]=>tiger,a[3]=>lion}


8) array_pop() - this function is used to remove values from array.


eg - 


<?php

$animal=array("cat","dog",''tiger","lion");
$a=array_push($animal);
print_r($a);
?>

output - output - array{a[0]=>cat,a[1]=>dog,a[2]=>tiger,}


9) array_shift() - this function remove first element of array.


eg - 


<?php

$animal=array("cat","dog",''tiger","lion");
$a=array_shift($animal);
print_r($a);
?>

output - output - array{a[0]=>cat,a[1]=>dog,a[2]=>tiger,a[3]=>lion}


10) array_unshift() - this function  insert new element in array and the new value will be inserted in the beginning  of the array.


eg - 


<?php

$animal=array("dog",''tiger","lion");
$a=array_unshift($animal,"cat");
print_r($a);
?>

output - output - array{a[0]=>cat,a[1]=>dog,a[2]=>tiger,a[3]=>lion}




ITERATOR  FUNCTION



Every php array keeps track of the current element the pointer to the current element it knows as iterator function.


1) current() - it returns the currently pointed elements.


2) reset() - its move the iterator to the first element.


3) next() - its move the iterator to the next element.


4) previous() - its move the iterator to the previous element.


5) end() - its move the iterator to last position.


6) key() - it returns the key of a element.
eg - 


<?php

$transport=array("foot","bike","car","plane");
$mode=current($transport);
$mode=next($transport);
$mode=current($transport);
$mode=previous($transport);

$mode=end($transport);
$mode=current($transport);
?>

output - foot
              bike
              bike 
              foot
              plane
              plane

Post a Comment

0 Comments