php

I need help in PHP array


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...
}

Solution

  • 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"