In this tutorial, you shall learn how to create an Associative array in PHP using array() function, with the help of example programs.

PHP – Create Associative Array

To create an Associative array in PHP, use array() function with the comma separated key-value pairs passed as arguments to the function.

Syntax

The syntax to create an associative array using array() function is

$myarray = array(key=>value, key=>value, key=>value)

In the above code snippet, array() function returns an associative array.

ADVERTISEMENT

Example

Let us write a PHP program, where we shall create an associative array $arr using array() function.

PHP Program

<?php
  $arr = ["apple"=>52, "banana"=>84, "cherry"=>12];
  print_r($arr);
?>

Output

PHP - Create Associative Array

Conclusion

In this PHP Tutorial, we have learned how to create an Associate array using array() function.