-3

My Array is -

Array
(
    [0] => accordion
    [1] => bars
 
)

I need to access this array in a simple way. Is it possible to like the below system?

if(isset($array['accordion'])){
// do something...
}
  • 3
    Arrays have keys and values. In your case the keys are 0 and 1, i.e. `$array[0]` and `$array[1]`. Please read the documentation for arrays for more info: https://www.php.net/manual/en/language.types.array.php – Alan Dec 21 '21 at 20:47
  • 1
    https://www.php.net/manual/en/function.in-array – Sammitch Dec 21 '21 at 20:58

1 Answers1

1

You access the array elements using a key.

The array in php can look something like this:

$my_array = ["foo", "bar"];

// These values of this array are accessed using the key - the numeric index of the key, where 0 is the first item in the array.

$my_array[0]; // points to the value "foo"
$my_array[1]; // points to the value "bar"

Or you can access the values of the array using named keys. Like this:

$my_array = [
   "name" => "John",
   "lastname" => "Doe"
];

// Then you approach the values of this array as follows:
$my_array['name'];  // points to the value "John"
$my_array['lastname']; // points to the value "Doe"
Petr Fořt Fru-Fru
  • 858
  • 2
  • 8
  • 23