Associative arrays in PHP. Create, add, change. Code example open

Associative arrays in PHP. Create, add, change. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

Similar to regular arrays but in which elements have their own key instead of the usual text index.

Create associative array:

$user = [
  "name" => 'Spike',
  "surname" => 'Johnson',
  "age" => 23
];

Show the element of the array:

echo $user['name'];//Spike

Multilevel array

Example:

$families = [["Tom", "Alice"], ["Bob", "Kate"]];
echo $families[0][0];  //Tom
echo $families[0][1];  //Alice
echo $families[1][0];  //Bob
echo $families[1][1];   //Kate

If you want to output an element of a multilevel Associative array, use:

echo $families[2]['name'];//turn to the second array and look for the element with the key "name";

Functions for working with arrays – https://www.php.net/manual/ref.array.php

0

More

Leave a Reply

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

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation