Specifying array – PHP

Specifying array – PHP

In PHP, an array is a collection of values that can be of different data types, such as strings, integers, or objects. Arrays in PHP can be specified using square brackets and can be assigned to variables.

To create an array with specific keys and values, you can use the array() function or shorthand square bracket notation. For example, the following code creates an array with keys “apple”, “banana”, and “orange” and their corresponding values:

$fruits = array( "apple" => "red", "banana" => "yellow", "orange" => "orange" );

You can access the values of an array using the array key. For example, to access the value of the “apple” key in the $fruits array, you can use the following code:

echo $fruits["apple"]; // Output: red

In addition to specifying an array with keys and values, you can also create indexed arrays where the keys are numeric indices. For example:

$numbers = array(1, 2, 3, 4, 5);

This creates an indexed array with values 1, 2, 3, 4, and 5. You can access the values of an indexed array using the numeric index. For example: echo $numbers[2]; // Output: 3

Apply for PHP Certification!

https://www.vskills.in/certification/certified-php-developer

Back to Tutorials

Share this post
[social_warfare]
Array basics – PHP
Accessing array elements in PHP

Get industry recognized certification – Contact us

keyboard_arrow_up