Q:

Create an associative array in PHP

belongs to collection: PHP Array Programs

0

Syntax:

    array(key1=>value1, key2=>value2, key3=>value3, ...);

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

In this example, we are creating an associative array, printing the values: 1) Using keys, and 2) Printing complete array with keys & values

<?php
	//creating student array with keys & values
	$std = array('id' => "101", 'name' => "Amit", 'course' => "B.Tech");

	//printing elemenets
	print ("std elements...\n");
	print ("Id = " . $std['id'] . " Name = " . $std['name'] . " Course = " . $std['course']);
	
	//printing array using for each loop
	
	//printing complete array
	print ("std...\n");
	print_r($std);
?>

Output

std elements...
Id = 101 Name = Amit Course = B.Techstd...
Array
(
    [id] => 101
    [name] => Amit
    [course] => B.Tech
)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How to get the only values from an associative arr... >>
<< Delete an element from an array in PHP...