How to create an associative array of state tax rates with one statement
$tax_rates = array('NC' => 7.75, 'CA' => 8.25, 'NY' => 8.875);
With multiple statements
$tax_rates = array();
$tax_rates['NC'] = 7.75;
$tax_rates['CA'] = 8.25;
$tax_rates['NY'] = 8.875;
How to create an associative array of country codes with one statement
$country_codes = array('DEU' => 'Germany', 'JPN' => 'Japan', 'ARG' => 'Argentina', 'USA' => 'United States');
With multiple statements
$country_codes = array();
$country_codes['DEU'] = 'Germany';
$country_codes['JPN'] = 'Japan';
$country_codes['ARG'] = 'Argentina';
$country_codes['USA'] = 'United States';
How to create an associative array of telephone extensions
$ext = array();
$ext[10] = 'Sales';
$ext[13] = 'Customer Service';
$ext[16] = 'Returns';
$ext[18] = 'Warehouse';
How to create an array that contains integer and string indexes
$employees = array();
$employees[0] = 'Mike';
$employees[1] = 'Anne';
$employees[2] = 'Judy';
$employees['senior'] = 'Mike';
$employees['newest'] = 'Pren';
How to use the print_r()
function to view an array
print_r($tax_rates); // Output: Array ( [NC] => 7.75 [CA] => 8.25 [NY] => 8.875 )
- An associative array uses a string as the index for the value that’s stored in the array. When using an associative array, the index is commonly called a key.
- An array with integer keys that have gaps between them can also be thought of as an associative array.
- Although not a good practice, an array can have both integer and string indexes.
Back