Tutorialspoint

PHP in array() function

php

<?php
$pro = array("Tom", "Amit", "Peter", "Rahul", 5, 10);
if (in_array(5, $pro, TRUE)) {
   echo "Match!";
} else {
   echo "No Match!";
}
?>

PHP end() function

php

<?php
$a = array('one', 'two', 'three', 'four', 'five', 'six' );
echo current($a)."\n";
echo next($a)."\n";
echo current($a)."\n";
echo end($a)."\n";
echo current($a)."\n";
?>

PHP current() function

php

<?php
$a = array('one', 'two', 'three', 'four', 'five', 'six' );
echo current($a)."\n";
echo next($a)."\n";
echo current($a)."\n";
?>

PHP compact() function

php

<?php
$name = "Tom";
$subject = "Hanks";
$id = "001";
$details = array("name", "subject");
$res = compact($details, "id");
print_r($res);
?>

PHP arsort() function

php

<?php
$rank = array("Australia"=>2,"India"=>5,"Bangladesh"=>9);
arsort($rank);

foreach($rank as $akey=>$avalue) {
   echo "Key=" . $akey . " : Value=" . $avalue;
   echo "<br>";
}
?>

PHP array unshift() function returns

php

<?php
$arr = array("p"=>"one","q"=>"two");
print_r(array_unshift($arr,"three"));
?>

PHP array uintersect() function

php

<?php
function compare_func($a, $b) {
   if ($a === $b) {
      return 0;
   }
   return ($a > $b)? 1:-1;
}
$arr1 = array("p"=>"one","q"=>"two","r"=>"three");
$arr2 = array("p"=>"three","q"=>"four","s"=>"three");
$res = array_uintersect($arr1, $arr2, "compare_func");
print_r($res);
?>

PHP array slice() function

php

<?php
$arr = array("electronics","accessories","shoes","toys","bags");
print_r(array_slice($arr,1,3, false));
?>

PHP array search() function

php

<?php
$arr = array(30, 2, 5, 7, 90, 35, 78);
echo array_search(35,$arr,true);
?>

PHP array reduce() function

php

<?php
function display($a1,$a2) {
   return $a1 + $a2;
}
$arr = array(50, 100, 150, 200, 250);
print_r(array_reduce($arr,"display",500));
?>

Advertisements
Loading...