PHP: Output An Associative Array

In this article I’ll show you, how to output each element of an an assciative array in PHP. First of all, we need an associative array. In this example I’ll use an array with colors as the key and the corresponding HEX-code of this color as the value.

Let’s start! Here is the associative PHP array:

// Our associative array
$arrMembers = array(
	'Red'=>'ff0000',
	'Green'=>'00ff00',
	'Blue'=>'0000ff',
	'Yellow'=>'ffff00'
);

Read morePHP: Output An Associative Array

PHP: Get A Random Item From An Array

In PHP it’s easy to work with arrays. Just declare someting like this:

<?php
$arrX = array("Kay", "Joe","Susan", "Frank");
?>

Now you can access the values with their index (which starts a 0 for the first item).

<?php
// output "Kay"
echo $arrX[0];
 
// output "Susan"
echo $arrX[2];
?>

PHP offers you several ways to get a random value of the array. One simple way is by using the array_rand() function. array_rand() expects the array as a parameter and returns a random index value as integer which then can be used to get the array value.

Read morePHP: Get A Random Item From An Array