Php array
From w3cyberlearnings
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
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys