Declare associative array PHP

As a general rule, to create an array you use PHP’s array() function, placing its various elements as parameters, separated by commas.

Updated: In modern versions of PHP to create an array in PHP it is enough to assign an empty array to a variable, like so:

Associative arrays are created like normal arrays, with the difference that when you specify the values ​​of the elements, you put both the index and the value to be stored in the array at that index and separate those two pieces of information with the “=>” characters.

For example, this would create an associative array with the indices “field1” and “other field”.

$my_associative_array = array( “field1” => “Value for the index field1”, “another field” => “value for the index other field” );

Updated: Currently this code can be summarized like this:

$my_associative_array = ;

To access elements of associative arrays, as you said, the string specified when making the declaration is used as an index.

$my_associative_array;

And to complement, I give you another example of an associative array declaration:

$otro_array_associativo = array(“name” => “Julio”, “surname” => “Díaz Cordero”, “age” => 23);

If we wanted to access its values, we would do it, for example, like this:

echo $other_associative_array . ” ” . $other_associative_array . ” have ” . $other_associative_array . ” years.”;

Now, you can also see this information in the chapter of del . Where you will also find other ways to declare associative arrays and even an example in which a two-dimensional associative array is created.

Loading Facebook Comments ...
Loading Disqus Comments ...