Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Friday, January 31, 2014

Calendar function in php - free coding

Calendar function in php - free coding 

example 

sms.php



Add the following  script before body of your file 


<Script language="JavaScript">
<!--


function GetDate(CtrlName){
        var cAssetValue=showModalDialog("calendar.php",0,"dialogWidth:175px;dialogHeight:210px");
Date_Fill(cAssetValue,CtrlName);
}


function Date_Fill(Mydate,Ctrl) {
var now = new Date(Mydate);
var day = now.getDate();
var month = now.getMonth() + 1;
day = ((day >9) ? "" : "0") + day;
month = ((month >9) ? "" : "0") + month;
var year = now.getYear();
if (year <= 99) {
year = "19" + year;
}
document.form1.item(Ctrl).value=day + "-" + month + "-" + year;
form1.action = "sms.php";
form1.target = "_parent";
form1.submit();

}



-->
</script>



<body>
<?php
if ($_POST['From_Date'] == "") {
$from_date = date("d-m-Y");
}
else {
$from_date = $_POST['From_Date'];

}
list($day1, $month1, $year1) = split('[-,/]', $from_date);

$From = $year1 . "-" . $month1 . "-" . $day1;

?>

<form name="form1" action="sms.php" method="post" >
<input type="hidden" name="hidvar1" >

 Select Date  <input id="myTextBox" type="text" name="From_Date" value="<?php echo $from_date;?>" size="12" maxlength="12" unselectable="on"><img src="/pics/Icon_calendar.gif" border="0" style="cursor: hand" alt="Calender" onclick="GetDate('From_Date')">

</form>
</body>




calendar.php file coding 

copy the following lines and save as calendar.php


<?php
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
?>
<html>
<!-- Creation date: 15-07-2003 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Calender</title>

<Script language="VbScript">
<!--
Dim CellSpan,MaxDay,SDate,CMonth,C_Date,NMonth,NDate,LDate,Scell,SDay,SDayNo
C_Date=Date
returnValue=Date

Sub Year_Change()
if Len(Year(C_Date))<=2 then
if Year(C_Date)>50 then
MyStr=1900+Year(C_Date)
Else
MyStr=2000+Year(C_Date)
End if
YearView.Value=MyStr
Else
YearView.Value=Year(C_Date)
End if
End Sub

Sub MonthView_OnChange()
MyDate=Monthview.Value & " 1, " & Year(C_Date)
C_Date=CDate(MyDate)
MonthTxt.Value=Month(C_Date)
YearTxt.Value=Year(C_Date)
SelDate.Value=Dateserial(YearTxt.Value,MonthTxt.Value,DayTxt.Value)
Change_Days()
if Month(C_Date)=Month(Date) then
Call Mark_CurrDate()
End if
End sub

Sub YearView_OnChange()
C_Date=Dateserial(YearView.Value,Month(C_Date),1)
MonthTxt.Value=Month(C_Date)
YearTxt.Value=Year(C_Date)
SelDate.Value=Dateserial(YearTxt.Value,MonthTxt.Value,DayTxt.Value)
Change_Days()
if Month(C_Date)=Month(Date) then
Call Mark_CurrDate()
End if
End sub

Sub Mark_CurrDate()
if Year(C_Date) <> Year(Date) then
Exit sub
End if
Call Year_Change()
MonthView.Value=Monthname(Month(C_Date))
B=cStr(Day(Date))
For A=1 to 35
Set Astr=document.all.item("Cell"&A)
if B = Astr.value then
Astr.style.color="Blue"
Astr.Style.Fontweight ="bold"
End if
Set Astr=Nothing
Next
End Sub

Sub Cell_Click(CNo)
if PrvCell.Value <> "" then
Set Astr=document.all.item(PrvCell.Value)
if Ucase(Astr.style.Color) <> "BLUE" then
Astr.style.color="Black"
Astr.Style.Fontweight ="Normal"
End if
Set Astr=Nothing
End if
Set Astr=document.all.item("Cell"&CNo)
PrvCell.Value="Cell"&CNo
if Ucase(Astr.style.Color) <> "BLUE" then
Astr.style.color="Red"
Astr.Style.Fontweight ="bold"
End if
DayTxt.Value=Astr.Value
Set Astr=Nothing
SelDate.Value=Dateserial(YearTxt.Value,MonthTxt.Value,DayTxt.Value)
End sub

Sub Cell_DblClick(CNo)
Set Astr=document.all.item("Cell"&CNo)
Astr.style.color="Blue"
Astr.Style.Fontweight ="bold"
DayTxt.Value=Astr.Value
Set Astr=Nothing
returnValue=Dateserial(YearTxt.Value,MonthTxt.Value,DayTxt.Value)
Window.Close
End sub


Sub Back_OnClick
if Month(C_Date)=1 then
C_Date=Dateserial(Year(C_Date)-1,12,1)
Call Year_Change()
Else
C_Date=Dateserial(Year(C_Date),Month(C_Date)-1,1)
End if
MonthTxt.Value=Month(C_Date)
YearTxt.Value=Year(C_Date)
SelDate.Value=Dateserial(YearTxt.Value,MonthTxt.Value,DayTxt.Value)
Change_Days()
if Month(C_Date)=Month(Date) then
Call Mark_CurrDate()
End if
End sub

Sub Front_OnClick
if Month(C_Date)=12 then
C_Date=Dateserial(Year(C_Date)+1,1,1)
Call Year_Change()
Else
C_Date=Dateserial(Year(C_Date),Month(C_Date)+1,1)
End if
MonthTxt.Value=Month(C_Date)
YearTxt.Value=Year(C_Date)
SelDate.Value=Dateserial(YearTxt.Value,MonthTxt.Value,DayTxt.Value)
Change_Days()
if Month(C_Date)=Month(Date) then
Call Mark_CurrDate()
End if
End Sub

Sub Change_Days()
MonthView.Value=Monthname(Month(C_Date))
SDayNo=1
CellSpan=7
NMonth=Month(C_Date)+1
NDate=Dateserial(Year(C_Date),NMonth,1)
LDate=NDate-1
MaxDay=Day(LDate)
SDate=Dateserial(Year(C_Date),Month(C_Date),1)
Sday=WeekDay(SDate)
CellRet="No"
if MaxDay=31 and Sday=7 then
Set Astr=document.all.item("Cell1")
Astr.Value="30"
Astr.style.color="Black"
Astr.Style.Fontweight ="Normal"
Astr.Disabled=False
Set Astr=Nothing
Set Astr=document.all.item("Cell2")
Astr.Value="31"
Astr.style.color="Black"
Astr.Style.Fontweight ="Normal"
Astr.Disabled=False
Set Astr=Nothing
CellRet="Yes"
Elseif Maxday=30 and Sday=7 then
Set Astr=document.all.item("Cell1")
Astr.Value="30"
Astr.style.color="Black"
Astr.Style.Fontweight ="Normal"
Astr.Disabled=False
Set Astr=Nothing
Set Astr=document.all.item("Cell2")
Astr.Value=""
Astr.Disabled=True
Set Astr=Nothing
CellRet="Yes"
Elseif Maxday=31 and Sday=6 then
Set Astr=document.all.item("Cell1")
Astr.Value="31"
Astr.style.color="Black"
Astr.Style.Fontweight ="Normal"
Astr.Disabled=False
Set Astr=Nothing
Set Astr=document.all.item("Cell2")
Astr.Value=""
Astr.Disabled=True
Set Astr=Nothing
CellRet="Yes"
End if


For A=1 to 7
if Sday<=A then
Set Astr=document.all.item("Cell"&A)
Astr.Value=SDayNo
Astr.style.color="Black"
Astr.Style.Fontweight ="Normal"
Astr.Disabled=False
Set Astr=Nothing
SDayNo=SdayNo+1
Elseif A<3 then
if CellRet="No" then
Set Astr=document.all.item("Cell"&A)
Astr.Value=""
Astr.Disabled=True
Set Astr=Nothing
End if
Else
Set Astr=document.all.item("Cell"&A)
Astr.Value=""
Astr.Disabled=True
Set Astr=Nothing
End if
Next
For A=1 to 4
For B=1 to 7
CellSpan=CellSpan+1
if MaxDay>=SDayNo then
Set Astr=document.all.item("Cell"&CellSpan)
Astr.Value=SDayNo
Astr.style.color="Black"
Astr.Style.Fontweight ="Normal"
Astr.Disabled=False
Set Astr=Nothing
SDayNo=SdayNo+1
Else
Set Astr=document.all.item("Cell"&CellSpan)
Astr.Value=""
Astr.Disabled=True
Set Astr=Nothing
End if
Next
Next

End Sub

Sub OK_Onclick
returnValue=Dateserial(YearTxt.Value,MonthTxt.Value,DayTxt.Value)
Window.Close
End Sub

Sub Cancel_OnClick
returnValue=Date
Window.Close
End Sub
-->
</Script>

<script language="Javascript">

</script>
</head>

<body onload="Mark_CurrDate()">
<?php
$today = getdate();
?>
<input type="hidden" name="YearTxt" value='<?php echo date("Y"); ?>'>
<input type="hidden" name="MonthTxt" value='<?php echo date("m"); ?>'>
<input type="hidden" name="DayTxt" value='<?php echo date("d"); ?>'>
<input type="hidden" name="PrvCell" value="">
<center>
<table border="1" align="center" cellspacing="0"><tr><td><table border="0" cellspacing="0"><tr><td><input type="button" Name="Back" value="<<" style="background: transparent; width:10px; font:9px; color: Navy; border: none; cursor: hand"></td><td>
<select name="MonthView" style="background: transparent; width: 75px; color: navy; font: 10px">
<?php
for ($a=1; $a <= 12; $a++) {?>
<option value='<?php echo date("F", mktime(0,0,0,$a,1,2000));?>'><?php echo date("F", mktime(0,0,0,$a,1,2000));?></option>
<?php } ?>
</select></td><td>
<select name="YearView" style="background: transparent; width: 48px; color: navy; font:10px">
<?php for ($a=1951; $a <= 2049; $a++) { ?>
<option value='<?php echo $a; ?>'> <?php echo $a; ?></option>
<?php } ?>
</select></td><td>
<input type="button" Name="Front" value=">>" style="background: transparent; width: 11px; font:9px; color: navy; border: none; cursor: hand"></td></tr></table></td></tr>
<tr><td>
<Table border="1" align="center" cellspacing="0">

<!--Calendar Day Names Header Starts Here -->

<tr>
<?php for ($a=1; $a <= 7; $a++) {?>
<th align="center" style="font: 11px; color: navy"><?php echo date("D", mktime(0,0,0,10,$a,2000));?></th>
<?php } ?>
</tr>

<!--Calendar Day Numbers Starts Here -->

<?php
$SDayNo = 1;
$CellSpan = 7 ;
$NMonth = date("m") + 1;
$NDate = getdate(mktime(0,0,0,$NMonth,1,date("Y")));
$LDate=getdate(mktime(0,0,0,$NMonth,1-1,date("Y")));
$MaxDay=date("d", mktime(0,0,0,$NMonth,1-1,date("Y")));
$SDate=getdate(mktime(0,0,0,date("m"),1,date("Y")));
$Sday=$SDate["wday"]+1;
?>
<tr>
<?php $CellRet = "No";
 if ($MaxDay == 31 and $Sday == 7){
 ?>
<td align="center"><input type="button" Name="Cell1" value="30" style="background: transparent;  width: 15px; height: 15px;  font: 9px; border: none; cursor: hand; font: xx-small;" onclick="Cell_Click(1)" ondblclick="Cell_DblClick(1)"></td>
<td align="center"><input type="button" Name="Cell2" value="31" style="background: transparent;  width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick=Cell_Click(2) ondblclick=Cell_DblClick(2)></td>
<?php $CellRet = "Yes";
}
elseif ($MaxDay == 30 and $Sday == 7) {
?>
<td align="center"><input type="button" Name="Cell1" value="30" style="background: transparent;  width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick="Cell_Click('1')" ondblclick="Cell_DblClick('1')"></td>
<td align="center"><input type="button" Name="Cell2" value="" style="background: transparent; width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick="Cell_Click('2')" ondblclick="Cell_DblClick('2')" disabled></td>
<?php $CellRet = "Yes";
}
elseif ($MaxDay == 31 and $Sday == 6) {
?>
<td align="center"><input type="button" Name="Cell1" value="31" style="background: transparent;  width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick="Cell_Click('1')" ondblclick="Cell_DblClick('1')"></td>
<td align="center"><input type="button" Name="Cell2" value="" style="background: transparent; width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick="Cell_Click('2')" ondblclick="Cell_DblClick('2')" disabled></td>
<?php $CellRet = "Yes" ;
}
?>

<?php for ($a=1; $a <= 7; $a++){
if ($Sday <= $a) {
?>
<td align="center"><input type="button" Name='Cell<?php echo $a; ?>' value='<?php echo $SDayNo; ?>' style="background: transparent; width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick="Cell_Click(<?php echo "'" . $a . "'"; ?>)" ondblclick="Cell_DblClick(<?php echo "'" . $a . "'"; ?>)"></td>
<?php $SDayNo = $SDayNo +1;
}
elseif ($a < 3) {
if ($CellRet == "No") {
?>
<td align="center"><input type="button" Name='Cell<?php echo $a; ?>' value="" style="background: transparent; width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick="Cell_Click(<?php echo "'" . $a . "'"; ?>)" ondblclick="Cell_DblClick(<?php echo "'" . $a . "'"; ?>)" disabled></td>
<?php
}
}
else {
?>
<td align="center"><input type="button" Name='Cell<?php echo $a; ?>' value="" style="background: transparent; width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick="Cell_Click(<?php echo "'" . $a . "'"; ?>)" ondblclick="Cell_DblClick(<?php echo "'" . $a . "'"; ?>)" disabled></td>
<?php
}
}
?>
<!--Filling Dates -->
</tr>
<?php for ($a=1; $a <=4; $a++) {
?>
<tr>
<?php for ($b=1; $b <= 7; $b++) {
$CellSpan = $CellSpan + 1 ;
 if ($MaxDay >= $SDayNo) {
?>
<td align="center"><input type="button"  Name='Cell<?php echo $CellSpan; ?>' value='<?php echo $SDayNo; ?>' style="background: transparent; width: 15px; height: 15px;  font: 9px; border: none; cursor: hand" onclick="Cell_Click(<?php echo "'" . $CellSpan . "'"; ?>)" ondblclick="Cell_DblClick(<?php echo "'" . $CellSpan . "'" ;?>)"></td>
<?php $SDayNo = $SDayNo + 1 ;
}
else {
?>
<td align="center"><input type="button"  Name='Cell<?php echo $CellSpan; ?>' value="" style="background: transparent;width: 15px; height: 15px; font: 9px; border: none; cursor: hand" onclick="Cell_Click(<?php echo "'" . $CellSpan . "'"; ?>)" ondblclick="Cell_DblClick(<?php echo "'" . $CellSpan . "'"; ?>)" disabled></td>
<?php }
}
?>
</tr>
<?php
}
?>

</table>
<input type="hidden" name="SelDate" size="20" maxlength="15" disabled style="background: transparent; text-align: center; border: none" align="absmiddle"  Value='<?php echo (date("d") . " " . date("M") . " " . date("Y"));?>'>
</td></tr></table>
</center>

</body>

</html>

Wednesday, December 11, 2013

The explode Function in PHP

PHP Code:

$rawPhoneNumber = "800-555-5555"; 

$phoneChunks = explode("-", $rawPhoneNumber);
echo "Raw Phone Number = $rawPhoneNumber <br />";
echo "First chunk = $phoneChunks[0]<br />";
echo "Second chunk = $phoneChunks[1]<br />";
echo "Third Chunk chunk = $phoneChunks[2]";

Display:

Raw Phone Number = 800-555-5555
First chunk = 800
Second chunk = 555
Third Chunk chunk = 5555 
 
 
 

explode Function - Setting a Limit

PHP Code:

$someWords = "Please don't blow me to pieces."; 

$wordChunks = explode(" ", $someWords);
for($i = 0; $i < count($wordChunks); $i++){
 echo "Piece $i = $wordChunks[$i] <br />";
}

$wordChunksLimited = explode(" ", $someWords, 4);
for($i = 0; $i < count($wordChunksLimited); $i++){
 echo "Limited Piece $i = $wordChunksLimited[$i] <br />";
}

Display:

Piece 0 = Please
Piece 1 = don't
Piece 2 = blow
Piece 3 = me
Piece 4 = to
Piece 5 = pieces.
Limited Piece 0 = Please
Limited Piece 1 = don't
Limited Piece 2 = blow
Limited Piece 3 = me to pieces.

 

 

Friday, December 6, 2013

Calling javascript function from php

Calling javascript function from php

I am trying to call a javascript function from php. According to all of the examples I have been looking at the following should work but it doesn't. Why not?

  
<?php
    echo "function test";
    echo '<script type="text/javascript">    run();      </script>';
?> 
 
 <html>
    <script type="text/javascript">
        function run(){
            alert("hello world");
        }
    </script>
</html>

 

Monday, December 2, 2013

Time Difference in Php

How to calculate minute difference between two date-times in PHP?


<?

$to_time = strtotime("2008-12-13 10:42:00");
$from_time = strtotime("2008-12-13 10:21:00");
echo round(abs($to_time - $from_time) / 60,2). " minute";
 
 ?>
 
 
 
<?php
$date1 = time();
sleep(2000);
$date2 = time();
$mins = ($date2 - $date1) / 60;
echo $mins;
?>
 

php function to remove special characters

 php function to remove special characters
 
 
function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
 
 
 
 
how can I prevent multiple hyphens from being next to each other? 
and have them replaced with just 1? Thanks in advance! 

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

   return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

Tuesday, September 17, 2013

PHP function to get age for date of birth

 PHP function to get age for date of birth  
 
function age_from_dob($dob){
 
$dob = strtotime($dob);
$y = date('Y', $dob);
 if (($m = (date('m') - date('m', $dob))) < 0) {
  $y++;
 } elseif ($m == 0 && date('d') - date('d', $dob) < 0) {
  $y++;
 }
return date('Y') - $y;
 } 
 
 
 
 echo age_from_dob('2005/04/19'); date in yyyy/mm/dd format.

Tuesday, August 20, 2013

substring_index string function

substring_index string function

Substring_index function returns us a substring from the main string from a landmark which is given by delimiter. From the delimiter to left or right ( towards staring or ending ) of the string can be given by count. If count is positive then everything towards left from final delimiter ( from left ) is returned. If the count is negative then everything towards right from final delimiter is returned.

Here is the syntax

substring_index(string,"delimiter”,count )

We will apply this to our newsletter subscriber table where we stored email address of our subscribers. Here we will apply substring_index to separate domain part and userid part and maintain a list. Here is a sample of email address.

userid@domainname.com

Here is the query to get the

select email,substring_index(email,"@",-1) from newsletter_table

This will give output as

userid@aol.com aol.com
onemore@gmail.com gmail.com

Like this the full list can be displayed. We can use count and group by command to generate a query to which can tell us number of subscribers in each domain. Say how many have yahoo account, how many have gmail account etc….

SELECT count(substring_index(email,"@",-1) ) as no, email FROM `newsletter_table` group by substring_index(email,"@",-1) order by no desc

Friday, July 19, 2013

Get Remote IP Address in PHP

 Get Remote IP Address in PHP
 
 
 
 
 
function getRemoteIPAddress() {
    $ip = $_SERVER['REMOTE_ADDR'];
    return $ip;
}

Thursday, March 21, 2013

PHP Functions

The real power of PHP comes from its functions.
In PHP, there are more than 700 built-in functions.

PHP Built-in Functions

For a complete reference and examples of the built-in functions, please visit our PHP Reference.

PHP Functions

In this chapter we will show you how to create your own functions.
To keep the script from being executed when the page loads, you can put it into a function.
A function will be executed by a call to the function.
You may call a function from anywhere within a page.

Create a PHP Function

A function will be executed by a call to the function.

Syntax

function functionName()
{
code to be executed;
}
PHP function guidelines:
  • Give the function a name that reflects what the function does
  • The function name can start with a letter or underscore (not a number)

Example

A simple function that writes my name when it is called:
<html>
<body>

<?php
function writeName()
{
echo "Kai Jim Refsnes";
}

echo "My name is ";
writeName();
?>

</body>
</html>
Output:
My name is Kai Jim Refsnes


PHP Functions - Adding parameters

To add more functionality to a function, we can add parameters. A parameter is just like a variable.
Parameters are specified after the function name, inside the parentheses.

Example 1

The following example will write different first names, but equal last name:
<html>
<body>

<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br>";
}

echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>

</body>
</html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.

Example 2

The following function has two parameters:
<html>
<body>

<?php
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br>";
}

echo "My name is ";
writeName("Kai Jim",".");
echo "My sister's name is ";
writeName("Hege","!");
echo "My brother's name is ";
writeName("Ståle","?");
?>

</body>
</html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?


PHP Functions - Return values

To let a function return a value, use the return statement.

Example

<html>
<body>

<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}

echo "1 + 16 = " . add(1,16);
?>

</body>
</html>
Output:
1 + 16 = 17

Sunday, September 9, 2012

PHP Function array_flip()

Syntax

array array_flip ( array $input );

Definition and Usage

array_flip() returns an array in flip order, i.e. keys from input become values and values from input become keys.
If a value has several occurrences, the latest key will be used as its values, and all others will be lost.

Paramters

ParameterDescription
inputThe array to be fliped

Return Values

Returns FALSE if it fails otherwise fliped array.

Example

Try out following example:
<?php
$array = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);

print_r(array_flip($array));
?> 
This will produce following result:
Array ( [1] => a [2] => b [3] => c [4] => d [5] => e)

PHP Function array_filter()

Syntax

array array_filter ( array $input [, callback $callback] );

Definition and Usage

Iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

Paramters

ParameterDescription
inputThe array to iterate over
callbackThe callback function to use
If no callback is supplied, all entries of input equal to FALSE will be removed.

Return Values

Returns the filtered array.

Example

Try out following example:
<?php
function odd($var)
{
    return($var & 1);
}

function even($var)
{
    return(!($var & 1));
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?> 
This will produce following result:
Odd :
Array ( [a] => 1 [c] => 3 [e] => 5)
Even:
Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12)

PHP Function array_fill_keys()

Syntax

array array_fill_keys ( array $keys, mixed $value );

Definition and Usage

Fills an array with the value of the value parameter, using the values of the keys array as keys.

Paramters

ParameterDescription
keysArray of values that will be used as keys
valueEither an string or an array of values

Return Values

Returns the filled array

Example

Try out following example:
<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a)
?> 
This will produce following result:
Array
(
    [foo] => banana
    [5] => banana
    [10] => banana
    [bar] => banana
)

PHP Function array_fill()

Syntax

array array_fill ( int $start_index, int $num, mixed $value );

Definition and Usage

Fills an array with num entries of the value of the value parameter, keys starting at the start_index parameter.

Paramters

ParameterDescription
start_indexThe first index of the returned array
numNumber of elements to insert
valueValues to use filling

Return Values

Returns the filled array

Example

Try out following example:
<?php
$a = array_fill(5, 6, 'apple');
print_r($a)
?> 
This will produce following result:
Array
(
    [5]  => apple
    [6]  => apple
    [7]  => apple
    [8]  => apple
    [9]  => apple
    [10] => apple
)

Friday, September 7, 2012

PHP Function array_diff_ukey()

Syntax

array_diff_ukey ( $array1, $array2 [, $array3...,callback $key_compare_func] );

Definition and Usage

Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
Unlike array_diff_key() an user supplied callback function is used for the indices comparision, not internal function.

Paramters

ParameterDescription
array1Required. The first array is the array that the others will be compared with.
array2Required. An array to be compared with the first array
array3Optional. An array to be compared with the first array
key_compare_funcRequired. callback function to use. The callback function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Return Values

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

Example

Try out following example:
<?php
function key_compare_func($v1,$v2) 
{
if ($v1===$v2)
 {
 return 0;
 }
if ($v1>v2)
 {
 return 1;
 }
else
 {
 return -1;
 }
}
$array1 = array(0=>"banana", 1=>"orange", 2=>"grapes");
$array2 = array(3=>"apple",1=>"apricot", 5=>"mango");
print_r(array_diff_ukey($array1,$array2,"key_compare_func"));
?> 
This will produce following result:
Array ( [0]=>banana [2]=>grapes )

PHP Function array_diff_uassoc()

Syntax

array_diff_uassoc ( $array1, $array2 [, $array3..., callback $key_compare_func] );

Definition and Usage

Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are used in the comparision.
Unlike array_diff_assoc() an user supplied callback function is used for the indices comparision, not internal function.

Paramters

ParameterDescription
array1Required. The array to compare from
array2Required. An array to be compared with the first array
array3Optional. An array to be compared with the first array
key_compare_funcRequired. callback function to use. The callback function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Return Values

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

Example

Try out following example:
<?php
function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", 
                                    "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);

?> 
This will produce following result:
Array ( [b] => brown [c] => blue [0] => red )

PHP Function array_diff_key()

Syntax

array array_diff_key ( array $array1, array $array2 [, array $...] );

Definition and Usage

Compares array1 against array2 and returns the difference.

Paramters

ParameterDescription
array1Required. The first array is the array that the others will be compared with.
array2Required. An array to be compared with the first array
array3Optional. An array to be compared with the first array

Return Values

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

Example

Try out following example:
<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'purple' => 3);

$array2 = array('blue' => 4, 'yellow' => 5, 'cyan'  => 6);

var_dump(array_diff($input_array1, $input_array2));
?> 
This will produce following result:
array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(3)
}

PHP Function array_diff_assoc()

Syntax

array array_diff_assoc( array $array1, array $array2 [, array $array3...] );

Definition and Usage

Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are used in the comparision.

Paramters

ParameterDescription
array1Required. The array to compare from
array2Required. An array to be compared with the first array
array3Optional. An array to be compared with the first array

Return Values

Returns an array containing all the values from array1 that are not present in any of the other arrays with the same keys.

Example

Try out following example:
<?php
$input_array1 = array( a=>"orange", b=>"mango", c=>"banana");
$input_array2 = array( a=>"orange", b=>"apple", c=>"banana");
print_r(array_diff_assoc($input_array1, $input_array2));

$input_array1 = array( a=>"orange", b=>mango", c=>"banana");
$input_array2 = array( a=>"banana", b=>"apple", c=>"orange");
print_r(array_diff_assoc($input_array1, $input_array2));
?> 
This will produce following result:
Array ( [b] => mango )
Array ( [a] => orange [b] => mango [c] => banana )

PHP Function array_diff()

Syntax

array array_diff ( array $array1, array $array2 [, array $array3 ...] );

Definition and Usage

Compares array1 against array2 and returns the difference.

Paramters

ParameterDescription
array1Required. The first array is the array that the others will be compared with.
array2Required. An array to be compared with the first array
array3Optional. An array to be compared with the first array

Return Values

Returns an array containing the differennces.

Example

Try out following example:
<?php
$input_array1 = array("orange", "banana", "apple");
$input_array2 = array("orange", "mango", "apple");
print_r(array_diff($input_array1, $input_array2));
?> 
This will produce following result:
Array ( [1] => banana )

PHP Function array_count_values()

Syntax

array array_count_values ( array $input );

Definition and Usage

Returns an array using the values of the input array as keys and their frequency in input as values.

Paramters

ParameterDescription
inputThe array of values to count

Return Values

Returns an assosiative array of values from input as keys and their count as value.

Example

Try out following example:
<?php
$input_array = array("orange", "mango", "banan", "orange" );
print_r(array_count_values($input_array));
?> 
This will produce following result:
Array ( [orange] => 2 [mango] => 1 [banana => 1 )

PHP Function array_combine()

Syntax

array array_combine ( array $keys, array $values );

Definition and Usage

Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

Paramters

ParameterDescription
keysArray of keys to be used
valuesArray of values to be used

Return Values

Returns the combined array, FALSE if the number of elements for each array isn't equal or if the arrays are empty.

Example

Try out following example:
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?> 
This will produce following result:
Array([green] => avocado [red] => apple [yellow] => banana)

Labels

AJAX (1) Answers (1) Apache (1) Array (16) bug (1) C (1) C++ (1) Calendar (1) Class (1) Commands (1) Cookies (2) Database (2) Date (7) Days (1) explode (1) File Upload (1) FILES (1) firewall (1) fix (1) Functions (26) GET (1) GMT (1) JavaScript (2) localhost (1) Mail (1) Menu (1) MYSQL (13) PERL (1) PHP (36) php.ini (1) POST (1) preg_match (1) Questions (1) Script (1) SMS (2) Solution (1) String (1) Time (5) Time Zone (1) Vista (1) Wamp Server (1) windows 7 (2) XML (1)

Popular Posts

Popular Posts