working with arrays part one

Problem

You want to work with string index arrays. Sometimes called Hashes.



Solution

PHP can handle number or string indexed arrays.

Arrays can be initialized in a number of ways. For example:

Basic array assignment and initialization, without specify numbers or strings:

<?php

$names[]="joe";
$names[]="john";
$names[]="jim";

?>



Example


Specifically assigning values to numbered elements in the names array:

<?php

$names[0]="joe";
$names[1]="john";
$names[2]="jim";

?>

Quicker way to assign values to names array:

$names=array("joe","john","jim");

Assigning values to a string indexed array – or hash (key and value pairs):

<?php

$addresses=array(
"joe" => "1 Marble Street",
"john" => "12 Marble Street",
"jim" => "23 Marble Street"
);

?>



Reference

[tags]PHP, Arrays, PHP Coding School[/tags]



Leave a Reply

Your email address will not be published. Required fields are marked *