Array

Example : How the contents of the Index Array are Displayed in Php?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		$vehicles = array("Scootie", "Motorcycle", "Cars", "Truck");
			echo $vehicles[0];
			echo $vehicles[1];
			echo $vehicles[2];
			echo $vehicles[3];
	   ?>
	</body>
</html>

-----------------  OR  ----------------

<!DOCTYPE html>
<html>
	<body>
	   <?php
	   
			$vehicles[0] ="Scootie";
			$vehicles[1] ="Motorcycle";
			$vehicles[2] ="Cars";
			$vehicles[3] ="Truck";		
			
			//echo $vehicles[0];
			//echo $vehicles[1];
			//echo $vehicles[2];
			//echo $vehicles[3];
			
			echo $vehicles[0],$vehicles[1],$vehicles[2],$vehicles[3];
			
	   ?>
	</body>
</html>

-----------------  OR  ----------------

<!DOCTYPE html>
<html>
	<body>
	   <?php	   
			$vehicles[0] ="Scootie";
			$vehicles[1] ="Motorcycle";
			$vehicles[2] ="Cars";
			$vehicles[3] ="Truck";		
		
			for ($i=0;$i<4;$i++)
			{
			   echo $vehicles[$i];
			}
	   ?>
	</body>
</html>

-----------------  OR  ----------------

<!DOCTYPE html>
<html>
	<body>
	   <?php	   
			$vehicles[0] ="Scootie";
			$vehicles[1] ="Motorcycle";
			$vehicles[2] ="Cars";					
			
			$len=count($vehicles);
			
			for ($i=0;$i<$len;$i++)
			{
				echo $vehicles[$i];
			}
	   ?>
	</body>
</html>

Output:
ScootieMotorcycleCarsTruck

Example : How are the contents of an Array Count in Php?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		$vehicles = array("Scootie", "Motorcycle", "Cars", "Truck");
		echo count($vehicles);
	   ?>
	</body>
</html> 

Output:
4

Example : How the contents of an Array are Sort in Ascending Order in Php?

<!DOCTYPE html>
<html>
	<body>	   
	   <?php
	   
		$vehicles = array("Scootie", "Motorcycle", "Cars", "Truck");					
		sort($vehicles);
		$len=count($vehicles);			
		for ($i=0;$i<$len;$i++)
		{
			echo $vehicles[$i]." ";
		}
	   ?>	
	</body>
</html> 

Output:
Cars Motorcycle Scootie Truck
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	   
	   <?php
	   
		$value = array("200", "410", "5", "-415");					
		sort($value);

		$len=count($value);			
		for ($i=0;$i<$len;$i++)
		{
		   echo $value[$i]." ";
		}
	   ?>	
	</body>
</html> 

Output:
-415 5 200 410

Example : How the contents of an Array are Sort in Descending Order in Php?

<!DOCTYPE html>
<html>
	<body>	   
	   <?php
	   
		$vehicles = array("Scootie", "Motorcycle", "Cars", "Truck");					
		rsort($vehicles);
		$len=count($vehicles);			
		for ($i=0;$i<$len;$i++)
		{
			echo $vehicles[$i]. " ";
		}
	   ?>	
	</body>
</html> 

Output:
Truck Scootie Motorcycle Cars

-----------------  OR  ----------------

<!DOCTYPE html>
<html>
	<body>	   
	   <?php
	   
		$value = array("200", "410", "5", "-415");					
		rsort($value);

		$len=count($value);			
		for ($i=0;$i<$len;$i++)
		{
		   echo $value[$i]." ";
		}
	   ?>	
	</body>
</html>

Output:
410 200 5 -415

Example : How the contents of an Associative Array is displayed in Php?

<!DOCTYPE html>
<html>
	<body>
		<?php
	   
			$vehicles['val'] ="500";
			$vehicles['num'] ="200";
			$vehicles['digit'] ="50";					
			
			echo $vehicles['val'];
			echo "  "; 
			echo $vehicles['num'];
			echo "  "; 
			echo $vehicles['digit'];			
		?>
	</body>
</html>

Output :
   500 200 50

-----------------  OR  ----------------

<!DOCTYPE html>
<html>
	<body>
	   <?php   
			 
               $vehicles=array('val'=>"500",'num'=>"200",'digit'=>"50");
			
			echo $vehicles['val'];
			echo "  "; 
			echo $vehicles['num'];
			echo "  "; 
			echo $vehicles['digit'];			
           ?>
	</body>
</html>

Output :
   500 200 50

-----------------  OR  ----------------

<!DOCTYPE html>
<html>
	<body>
	   <?php   
			 
            $vehicles=array('val'=>"500",'num'=>"200",'digit'=>"50");
			
			foreach($vehicles as $m => $contents) 
			{
			echo "Value at " . $m . " Index is = " . $contents;
			echo "<br>";
			}			
           ?>
	</body>
</html>

Output :
Value at val Index is = 500
Value at num Index is = 200
Value at digit Index is = 50
Example: A Php program to use the array_chunck() php function to break the array into multiple sub-arrays.
<!DOCTYPE html>
  <html>
	<body>
		<?php 
			$flower=array ("rose", "marigold", "jasmine", "sunflower", "orchid", "tulip"); 
			print_r(array_chunk($flower,3)); 
			//echo(array_chunk($flower,3)); //Notice: Array to string conversion in C:\xampp\htdocs\test\test1.php on line 7 Array
		?> 
	</body>
  </html>

Output:

Array ( 
[0] => Array ( [0] => rose [1] => marigold [2] => jasmine )
[1] => Array ( [0] => sunflower [1] => orchid [2] => tulip ) 
)
Example: A Php program uses the array_reverse() php function to reverse the array contents.
<!DOCTYPE html>
<html>
	<body>
	  <?php	   
		$flower=array ("rose", "marigold", "jasmine", "sunflower", "orchid", "tulip");			
		$rev=array_reverse($flower); 
		foreach($rev as $val) 
		{ 
		   //echo "$val<br />";
                   echo $val." "; 
		}			
	  ?>
	</body>
</html>

Output:
tulip orchid sunflower jasmine marigold rose
Example: A Php program uses the array_search() php function to find the specific array elements.
<!DOCTYPE html>
<html>
	<body>
	  <?php	   
		$flower=array ("rose", "marigold", "jasmine", "sunflower", "orchid", "tulip");			
		$result=array_search("tulip",$flower); 
		echo $result; 			
	  ?>
	</body>
</html>

Output:
5
NB: If no value is matched/found then no index value is displayed.

String

Example : How to find the Length of a string/sentence in Php?
<!DOCTYPE html>
<html>
	<body>	
		<?php			
			echo strlen("Codershelpline is Your site");					
		?>
	</body>
</html>

Output :
27 
Example : How to find the number of Words in a string/sentence in Php?
<!DOCTYPE html>
<html>
	<body>	
		<?php			
		   echo str_word_count("Codershelpline is Your site");					
		?>
	</body>
</html>

Output :
4
Example : How to reverse the given String in Php?
<!DOCTYPE html>
<html>
	<body>	
		<?php			
			echo strrev("Codershelpline");					
		?>
	</body>
</html>
Output : enilplehsredoC
Example : How to  Search a Word in a given string/sentence in Php?
<!DOCTYPE html>
<html>
	<body>	
		<?php			
			echo strpos("Codershelpline.com","helpline");					
		?>
	</body>
</html>
Output :
6
Example : How to  Replace a Word in a given String/Sentence in Php?
<!DOCTYPE html>
<html>
	<body>
	   <?php
		echo str_replace("help", "India", "Codershelpline"); 
	   ?>
	</body>
</html> 

Output :
CodersIndialine
Example : How to  Find an ASCII character(using char() Php function) from ASCII value in Php?
<!DOCTYPE html>
   <html>
	<body>
	   <?php
		echo chr(65)."<br>";     // Print ASCII Char in Decimal Format.
                //$str=chr(65);
		//echo $str;
		echo chr(065)."<br>";    // Print ASCII Char in Octal Format.
		echo chr(0x65)."<br>";   // Print ASCII Char in Hexadecimal Format.	
	   ?>
	</body>
   </html>

Output:
A
5
e
()Example : How to convert a String into Array form(using explode() Php function) in Php?
<!DOCTYPE html>
	<html>
	   <body>
		<?php
			$str = "Codershelpline is our web site";
			print_r(explode(" ",$str));
		?>
	   </body>
	</html>

Output: Array ( [0] => Codershelpline [1] => is [2] => our [3] => web [4] => site )

Loading

Categories: Php Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.