Working With Arrays in PHP

Sanskruti's Avatar author of Working With Arrays in PHP
This is an article on Working With Arrays in PHP in PHP.
Arrays are common to many programming languages. They are special variables which can hold more than one value, each stored in its own numbered 'space' in the array. Instead of having many similar variables, you can store the data as elements in an array.Each element in the array has its own ID so that it can be easily accessed.

There are three different kind of arrays:
  • Numeric array - An array with a numeric ID key
  • Associative array - An array where each ID key is associated with a value
  • Multidimensional array - An array containing one or more arrays

Numeric Arrays



A numeric array stores each element with a numeric ID key. There are different ways to create a numeric array.

Example 1

In this example the ID key is automatically assigned:

Code:
$names = array("John","Paul","Steve");
Example 2

In this example we assign the ID key manually:

Code:
$names[0] = "John";
$names[1] = "Paul";
$names[2] = "Steve";
The ID keys can be used in a script in the following way

Code:
<?php
$names[0] = "John";
$names[1] = "Paul";
$names[2] = "Steve";
echo $names[1] . " and " . $names[2] . 
" are ". $names[0] . "'s Friends";
?>
The code above will output:

Paul and Steve are John's Friends

Associative Arrays



An associative array, each ID key is associated with a value.When storing data about specific named values, a numerical array is not always the best way to do it.With associative arrays we can use the values as keys and assign values to them.

Example 1

In this example we use an array to assign ages to the different persons:

$ages = array("John"=>32, "Paul"=>30, "Steve"=>34);

Example 2

This example is the same as example 1, but shows a different way of creating the array:

Code:
$ages['John'] = "32";
$ages['Paul'] = "30";
$ages['Steve'] = "34";
The ID keys can be used in a script:

Code:
<?php
$ages['John'] = "32";
$ages['Paul'] = "30";
$ages['Steve'] = "34";
echo "John is " . $ages['John'] . " years old.";
?>
The code above will output:

John is 32 years old.

Multidimensional Arrays



In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Example

In this example we create a multidimensional array, with automatically assigned ID keys:

Code:
$families = array
(
  "Griffin"=>array
  (
  "John",
  "Lois",
  "Megan"
  ),
  "Paul"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
);
The array above would look like this if written to the output when done through print_r function:

Code:
Array
(
[Griffin] => Array
  (
  [0] => John
  [1] => Lois
  [2] => Megan
  )
[Paul] => Array
  (
  [0] => Glenn
  )
[Brown] => Array
  (
  [0] => Cleveland
  [1] => Loretta
  [2] => Junior
  )
)
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
You can initialize Numeric Arrays like this also,

Code: PHP
$names = array();
$names[] = 'Pradeep';
$names[] = 'Shabbir';
$names[] = 'Manindar Poddar';
// PHP will automatically set the index
 

You can print associative arrays like this,

Code: PHP
$ages['John'] = "32";
$ages['Paul'] = "30";
$ages['Steve'] = "34";

//using sprintf or printf
printf("John is %d years old",$ages['John']);

// or this way
print("John is {$ages['John']} years old");
Scripting like this
0
shrikrishnatech's Avatar, Join Date: Nov 2010
Go4Expert Member
thanks for this code
0
Scripting's Avatar, Join Date: Jun 2010
John Hoder
Amazing ...
0
Bradpitt's Avatar, Join Date: Aug 2011
Newbie Member
Assuming that you have three arrays which might contain different values as follows.:

PHP Code:
$arr1 = array('1''5''10');
$arr2 = array('1''3''10');
$arr3 = array('1''6''10');
How would you strip out what's different and get it as follows?:

$arr1 = array('
1', '10');
$arr2 = array('
1', '10');
$arr3 = array('
1', '10');
I meant I wanted to get it as follows.:

$result = array('
1', '10'); 
______________________
0
mukeshsoftona's Avatar
Banned
Code:
<?php
function array_get_key_val($key, $heystack) {
  if (is_array($heystack)) {
    foreach ($heystack as $k => $v) {
      if ($k == $key) {
        return $v;
      }
      elseif (is_array($v)) {
        return array_get_key_val($key, $v);
      }
    }
  }
  return FALSE;
}
?>

Last edited by shabbir; 14Nov2011 at 12:00.. Reason: Code blocks
0
Scallar's Avatar, Join Date: Nov 2011
Newbie Member
<?php
function arrays($array1,$array2)
{
echo $array1.' '.$array2;
}
arrays(myname,lastname);
echo arays();
?>
Is this code is correct and where is the fault and how i can change it . i want that user enter value in it and it show its name and last name in an array in the database.
0
c_user's Avatar, Join Date: Aug 2009
Contributor
If u want that the user should enter the values and it should be displayed on the database. Then you have to accept the values from the form and use the command : mysql_query("INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)"); to insert into the database
0
designfirm's Avatar, Join Date: Jan 2012
Go4Expert Member
Nice post.......
0
donor's Avatar
Banned
<?php
$arr = array("foo" => "bar", 12 => true);

echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>