Skip to content

PHP Array & Strings

As in other languages, PHP also has three different kinds of arrays,

  1. Numerical Array.
  2. Associative Array.
  3. Multi-dimensional Array.

Declaration and Access of Arrays

Numerical Array

A key for the value of an array is automatically assigned. A numerical array stores each element with a numeric key. The

Example

<?php
	$my_blog = array("php","js","mysql","ajax");
	
	echo $my_blog[0];
	//prints the first element.
    
	print_r($my_blog);
	//prints the whole elements in array format.
	
	for($i=0;$i<count($my_blog);$i++)
{
echo $my_blog[$i];
}
//printing array using a for loop ?>

Associative Array

Each key is associated with a value. The below example shows, how to specify keys for associative arrays.

Example

<?php
$my_blog = array("one"=>"php",
	"two"=>"js",
	"three"=>"mysql",
	"four"=>"ajax");
					
	foreach($my_blog as $key=>$value)
	{
		echo $key." is key.";
		echo “<br>";
		echo $value." Is value";
		echo “<br><br>"; 
	}

Multi-Dimensional Array

Each element in the array can also be an array and each element in the sub-array can also be an array.

Example


<?php
	$my_blog = array("php", 
"js"=>array("one"=>"Unobstructive JS", "two"=>"Invocation Patterns"),
"mysql"=>array("one"=>"Stored Procedure", "two"=>"Triggers"),
"ajax");

foreach($my_blog as $key=>$value)
{
if(is_array($value))
{
echo $key . "<br />";

foreach($value as $sub=>$sub_value)
{
echo "---" . $sub_value . "<br />";
}
}
else
{
echo $value . "<br />";
}
}
//printing array elements using foreach ?>

Array & String Functions

We have hundreds of array and string functions, here are some sample functions for handling and manipulating the strings,

explode

Split a string by string.
Example:

<?php
	$str = "php, mysql, javascript, ajax";
	$exp = explode(",", $str);
	echo $exp[0]; echo $exp[1];
?>
implode

Join array element with in a string.
Example:

<?php
	$arr  = array("php", "mysql",
 "javascript", "ajax");
	$imp = implode(",", $arr);
	echo $imp;
?>
md5

Calculate md5 hash of a string.
Example:

<?php
	$hash  = md5("javascript");
	echo $hash;
 //de9b9ed78d7e2e1dceeffee780e2f919
?>
strlen

Calculate length of a string.
Example:

<?php
	$len  = strlen("javascript");
	echo $len; // 10
?>
htmlentities

Convert all applicable characters to HTML entities.
Example:

<?php
	$enty  = "<b>javascript</b>";
	echo htmlentities($enty);
 // &lt;b&ltgjavascript&lt;b&gt;
?>

More String Functions

in_array

Checks a value exists in an array.
Example:

<?php
	$arr = array("php", "mysql", 
	"javascript", "ajax");
	if(in_array("ajax", $arr))
		echo "yeahh… got it.";
	else
		echo "noo.. not found.";
?>
array_combine

Creates an array by using one arrays of keys and another for it value.
Example:

<?php
	$arr_one  = array("php", "mysql",
 	"javascript", "ajax");
	$arr_two = array("yes", "no", 
	"yes", "no");
	$fin_arr = array_combine($arr_one,
	$arr_two);
	
	Print_r($fin_arr);
	
	/* Array
	(
	[green]  => avocado
	[red]    => apple
	[yellow] => banana
	) */
?>
array_merge

Merge one or more arrays.
Example:

<?php
	$arr_one  = array("php", "mysql");
	$arr_two = array("javascript", 
	"ajax");
	$fin_arr = array_merge($arr_one, 
	$arr_two);
	Print_r($fin_arr);
	/* Array
	(
	[0] => php
	[1] => mysql
	[2] => javascript
	[3] => ajax
	) */
?>

More Array Functions

<< Back

PHP Super-Global Variables

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: