AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP Associative Array


<< Previous Page
HTML tutorial
Next Page >>
HTML tutorial

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:

$population = array("Madrid"=>"4146000", "Barcelona"=>"3712000", "Valencia"=>"2124000");

or

$population['Madrid'] = "4146000"
$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.

Example (this is HTML - PHP editor, change text on this window)
<?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;}

Example (this is HTML - PHP editor, change text on this window)

<?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?

Example (this is HTML - PHP editor, change text on this window)

<?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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...