> Online tutorial : array in php
Showing posts with label array in php. Show all posts
Showing posts with label array in php. Show all posts

date and time in php

date and time function allow you to extract and format the date and time on the server.
Note
These function depend on the locale settings of the server
date()

date() function formats a local time/date
The following describes the format arguments
  • a:am or pm
  • A:AM or PM
  • d:Numeric day of the month
  • D:Short day abbreviation
  • F:Full month name
  • g:12-hour time without leading zero
  • G:24-hour time without leading zero
  • h:12-hour time with leading zero
  • H:24-hour time without leading zero
  • i:mintues with leading zero
  • L:Boolean indicating a leap year
  • m:Numeric month with leading zero
  • M:Short month abbreviation
  • n:Numeric month without leading zero
  • O:GMT offset in[+]HHMM format
  • s:seconds
  • S.Suffix to numerical date
  • T:Time zone name
  • U:Unix seconds
  • w:Numeric day of the week
  • y:two digit year
  • Y:Four digit year

list() in php

list() function is used to assign values to a list of variable in one operation

syntax
list(var1,var2,...)


<?php

$sum=array("india","pak");
list($a,$b)=$sum;
echo "we are ,a $a,amd $b";


?>

Output

we are a india and pak

ksort() in php

ksort() function sorts an array by the keys.The values keep their original keys

syntax
ksort(array,sorttype)


<?php

$sum=array(a=>"india",b=>"pak");
ksort($sum);
print_r($sum);

?>

Output
Array(a=>india
b=>pak)

in_array() in php

in_array() function searched an array for a specific value.This function returns TRUE if the value is found in the array or FALSE otherwise.
syntax
in_array(search,array,type)


<?php

$sum=array("india","pak");
if(in_array("india",$sum)
{
echo"Match Found"
}
echo"Not found";
}

?>

Output
Match Found

count() in php

count() function counts the element of an array,of the properties or two properties of an array.
Syntax
count(array1,array2)
Example


<?php

$sum=array("india","pak");
// count the elements of an array
echo count($sum);

?>

Output
2

array_push() in php

array_push() function inserts one or more element to the end of an array
syntax
array_push(array,val1,val2,...)


<?php

$sum=array("india","pak");
array_push($arr,"usa","uk");

print_r($sum);

?>

Output
Array([0]=>india
[1]=>pak
[2]=>usa
[3]=>uk)

asort() in php

asort() function sorts an array by the values.The values keep their original keys.
syntax
asort(array,sorttype)


<?php
$sum=array(0=>"apple",1=>"orange",2=>"pine apple");
asort($sum);
print_r($sum);

?>

Output
Array([0]=>apple
[1]=>orange
[2]=>pine apple)

array_values() in php

The array_values() function returns the array containing all the values of an array

syntax
array_values(array)


<?php
$sum=array(0=>"apple",1=>"orange",2=>"pine apple");
// returns the array containing all the values of an array
print_r($array);

?>

Output
Array(0=>apple
1=>orange
2=>pine apple)

array_unique() in php

array_unique() function removes the duplicate values from an array.if two or more array values an the same ,the sirst apperence will be kept and the other will be removed
Syntax
arrray_unique(array)
Example

<?php
$sum=array(0=>"apple",1=>"orange",2=>"apple",);
echo "before using array unique";
print_r($sum);
echo "after using array unique";
print_r(array_unique($sum));

?>

Output
before using array unique

array(0=>apple
1=>orange
2=>apple)
after using array unique
array(0=>apple
1=>orange)

array_sum() in php

The array_sum() function returns the sum of all the values in the array

Syntax
array_sum(array)
Example

<?php
// sum the all the values in the array
$sum=array(0=>15,1=>15,1=>15,);
echo array_sum($sum);

?>

Output
45