Jump to: navigation, search

Php array

From w3cyberlearnings

Contents

PHP function array

This function creates single or multidimensional array.

Syntax array

  • element: array element
$array_name= array(element);

OR

$array_name[] = value

OR

$array_name[index] = value

OR

$array_name['key23']= value

OR

$array_name['name']= array(element)

Note

In order to access array content you can visit foreach() function.

Example 1: Create An Array

<?php

$name = array('Taylor', 'Lucas', 'Makayla', 'Jennifer');

?>

Example 2: Create array without assign index

  • Array index will automatically generate
<?php

$name[] = 'Taylor';
$name[] = 'Lucas';
$name[] = 'Makayla';
$name[] = 'Jennifer';

print_r($name);
?>

Example 3: Create assign index

  • Assign individual index
<?php

$name[0] = 'Taylor';
$name[1] = 'Lucas';
$name[2] = 'Makayla';
$name[3] = 'Jennifer';

?>

Example 4: Create associative array

<?php

$score= array(
	 'Taylor' => 32,
	 'Lucas' => 12,
	 'Makayla' => 42,
	 'Jennifer' => 12);
?>

Example 5: Create associative array manually

<?php

$score['Taylor']= 32;
$score['Lucas'] = 12;
$score['Makayla'] = 42;
$score['Jennifer'] = 12;
?>

Example 6: Create multidimensional array

$team = array(
	 'group 1'=> array('Jennifer','Nevaeh','Elizabeth'),
	 'group 2'=> array('Grace','Lauren','Chase'),
	 'group 3'=> array('Kimberly','Jade')
);

Example 7: create multidimensional array manually

<?php

$team['group 1'] = array('Jennifer', 'Nevaeh', 'Elizabeth');
$team['group 2'] = array('Grace', 'Lauren', 'Chase');
$team['group 3'] = array('Kimberly', 'Jade');

?>

Related Links


Navigation
Web
SQL
MISC
References