Pages



Definition and Usage

The array_diff_assoc() function compares the keys and values of two (or more) arrays, and returns the differences.
This function compares the keys and values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

Syntax

array_diff_assoc(array1,array2,array3...);
 
Example :

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");

$result=array_diff_assoc($a1,$a2);
print_r($result);
?>
 



OR :


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","f"=>"green","g"=>"blue");
$a3=array("h"=>"red","b"=>"green","g"=>"blue");

$result=array_diff_assoc($a1,$a2,$a3);
print_r($result);
?>

 
+PHP Development Outsourcing +PHP Developers +PHP Tutorials +PHP Programming +My PHP Developers +My PHP Developers


Example :

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_diff($a1,$a2);
print_r($result);
?>
 


OR


<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");

$result=array_diff($a1,$a2,$a3);
print_r($result);
?>

Syntax

array_diff(array1,array2,array3...);

+PHP Development Outsourcing +PHP Developers +PHP Tutorials +PHP Programming +My PHP Developers +My PHP Developers


Example :

<?php
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>  


Syntax

 array_count_values(array)

+PHP Development Outsourcing +PHP Developers +PHP Tutorials +PHP Programming +My PHP Developers +My PHP Developers

 
Example :


<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");

$c=array_combine($fname,$age);
print_r($c);
?>

Syntax

array_combine(namearray1,nameofarray2);

+PHP Development Outsourcing +PHP Developers +PHP Tutorials +PHP Programming +My PHP Developers +My PHP Developers
Example :

 




 In HTML File 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<form action="cases.php" method="post">

Type any word<input type="text" name="word"/>

<input name="submit" type="submit" value="Length"/>

</form>

</body>

</html>


Note : Enter the words in Text box "Haider Ali"





In Php File:


<?php

$word=$_POST['word'];

 echo trim($word,"Hai");


?>


Example Download....!

+PHP Development Outsourcing +PHP Developers +PHP Tutorials +PHP Programming +My PHP Developers +My PHP Developers
Example :

 




 In HTML File 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<form action="cases.php" method="post">

Type any word<input type="text" name="word"/>

<input name="submit" type="submit" value="Length"/>

</form>

</body>

</html>


In Php File:


<?php

$word=$_POST['word'];

 echo strlen($word);

?>


Example Download....!

+PHP Development Outsourcing +PHP Developers +PHP Tutorials +PHP Programming +My PHP Developers +My PHP Developers 

Example :

 




 In HTML File 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<form action="cases.php" method="post">

Type any word<input type="text" name="word"/>

<input name="submit" type="submit" value="lowercase"/>

</form>

</body>

</html>


In Php File:


<?php

$word=$_POST['word'];

 echo ucwords($word);

?>


Example Download....!

+PHP Development Outsourcing +PHP Developers  +PHP Tutorials +PHP Programming 
+My PHP Developers +phptutorial rs +phptutorial care +phptutorail +PHPtutorials 
+PHPTutorials +PHPtutorials +phptutorail 
Example :

 In HTML File 


<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<form action="cases.php" method="post">

Type any word<input type="text" name="word"/>

<input name="submit" type="submit" value="lowercase"/>

</form>

</body>

</html>


In Php File:


<?php

$word=$_POST['word'];

 echo ucfirst($word);

?>


Example Download....!

Upper Case Using PHP


Example :

 In HTML File 


<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<form action="cases.php" method="post">

Type any word<input type="text" name="word"/>

<input name="submit" type="submit" value="lowercase"/>

</form>

</body>

</html>


In Php File:

<?php

$word=$_POST['word'];

 echo strtoupper($word);

?>



Example Download....!


What is an Array?

An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

$cars1="Volvo";
$cars2="BMW";
$cars3="Toyota";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is to create an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.

Create an Array in PHP

In PHP, the array() function is used to create an array:

array();
In PHP, there are three types of arrays:

  • Indexed arrays - Arrays with numeric index
  • Associative arrays - Arrays with named keys
  • Multidimensional arrays - Arrays containing one or more arrays

PHP Indexed Arrays

There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0):

$cars=array("Volvo","BMW","Toyota");
or the index can be assigned manually:

$cars[0]="Volvo";
$cars[1]="BMW";
$cars[2]="Toyota";
The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:

Example


<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

Get The Length of an Array - The count() Function

The count() function is used to return the length (the number of elements) of an array:

Example


<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?> 

Loop Through an Indexed Array

To loop through and print all the values of an indexed array, you could use a for loop, like this:

Example


<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)
  {
  echo $cars[$x];
  echo "<br>";
  }
?> 

PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array: 

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
or:

$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
The named keys can then be used in a script:

Example


<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?> 

Loop Through an Associative Array

To loop through and print all the values of an associative array, you could use a foreach loop, like this:

Example

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

foreach($age as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
  }
?>

Flag Counter
| Copyright © 2013 Remote Tutor