PHP Access Arrays

To access an array item, refer to the index number for indexed arrays, and the key name for associative arrays.

Example
<!DOCTYPE html>
<html>
<body>
<pre>
<?php
$fruits = ["Apple", "Banana", "Cherry"];

// Accessing elements
echo $fruits[0]; // Outputs: Apple
echo $fruits[1]; // Outputs: Banana
?>
</pre>
</body>
</html>

Try it yourself

Accessing Associative Arrays


In an associative array, elements are accessed using custom keys.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$person = [
"name" => "John",
"age" => 30,
"city" => "New York"
];

// Accessing elements by keys
echo $person["name"]; // Outputs: John
echo $person["age"]; // Outputs: 30
?>
</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.