working with arrays part one

[problem]

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

[/problem]

[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";

?>

[/solution]

[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"
);

?>

[/example]

[reference]

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

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

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