PHP Associative Array
PHP Tutorial » PHP Associative Arrays
What is an associative array PHP?, How do I get associative array in PHP?
Associative arrays are arrays that use named keys that you assign to them.
There are two types to create an associative array:
or
$population['Barcelona'] = "3712000"
$population['Valencia'] = "2124000"
copy and paste in the editor below, click "Execute" and notice the result
<?php
$population['Madrid'] = "4146000";
$population['Barcelona'] = "3712000";
$population['Valencia'] = "2124000";
echo " The population of Barcelona is" . $population['Barcelona'] . "citizens.";
?>
Creating an associative array of mixed types
Associative arrays are used to store key value pairs.
<?php
/* Creating an associative array of mixed types */
$my_arr["abc"] = 915;
$my_arr[107] = "mno";
$my_arr[12.36] = 121;
$my_arr["xyz"] = "Paris";
/* Looping through an array using foreach */
foreach ($my_arr as $key => $val){
echo $key."==>".$val."\n";
}
?>
Related subjects:
What is an array
Convert array to string comma
Array to comma separated
Tags: PHP Associative Arrays: to numeric, and indexed, push, to simple, tu string, get key, multidimensional, add element, key
Convert Associative Array To Multidimensional
To make rounded coners for we used CSS Style for .button { border: none; color: white; padding: 10px; text-align: center; text-decoration: none; display: inline-block; font-size: 14px; margin: 8px 4px; cursor: pointer; font-weight: bold;}
<?php
$a = ['ab' => 22, 'bc' => 12, 'cd' => 41, 'de' => 1102];$a1 = array_keys($a);
$a2 = array_values($a);$b = [];
for ($i=0; $i<count($a); $i++) {
array_push($b, [$a1[$i],$a2[$i]] );
}print_r($b); //[['ab', 22], ['bc', 12], ['cd', 41], ['de', 1102]];
?>
PHP array associative add element
How do you add an element to the associative array?
<?php
// Existing array
$my_arr = array('zero' => 0, 'one' => 1, 'two' => 2, 'three' => 3);
// New element
$my_arr['four'] = 4;
// Final array
print_r($my_arr);
?>
PHP Associative Arrays: to numeric, and indexed, push, to simple, tu string, get key, multidimensional, add element, key
PHP Associative Array - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.