-3

I have a php code.

$metadata = Array(
        'facebook'  => Array(
                'title'         => 'og:title',
                'type'          => 'og:type',
                'url'           => 'og:url',
                'thumbnail'     => 'og:image',
                'sitename'      => 'og:site_name',
                'key'           => 'fb:admins',
                'description'   => 'og:description'
            ),
        'google+'   => Array(
                'thumbnail'     => 'image',
                'title'         => 'name',
                'description'   => 'description'
            ),
        'twitter'   => Array(
                'card'          => 'twitter:card',
                'url'           => 'twitter:url',
                'title'         => 'twitter:title',
                'description'   => 'twitter:description',
                'thumbnail'     => 'twitter:image'
            )
    );

what does the => means. How to access an element in this array.

Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • this is array .. $metadata['facebook']['type'] will give you 'og:type' – Eddie Apr 03 '14 at 07:02
  • echo $metadata['facebook']['title']; – Leo Pflug Apr 03 '14 at 07:03
  • An array is a key/value store: when working with an array, the value ___before___ the `=>` is the `key`; the value ___after___ the `=>` is the `value` – Mark Baker Apr 03 '14 at 07:03
  • @ShankarDamodaran: Actually, none of the questions linked from that reference (including that explicitly linked by @DaveChen) ask about the use of `=>` during array declaration. – eggyal Apr 03 '14 at 07:17

2 Answers2

0

You have a multi dimensional array, so facebook, google and twitter are elements of the 1st dimension of the $metadata array and they are arrays themselves, here lies the multi dimension.

The => is just like an arrow pointing to the value/data.

To access the 1st dimension would be $metadata['twitter']; or $metadata[2]; it's the same statement, this would bring back the elements/keys of the twitter array.

To access the 2nd dimension would be $metadata['twitter']['card']; or $metadata[2][0]; again the two statements are the same, this would bring back the value of the element/key card

Frank
  • 11
  • 3
0
  1. what does the => means.

    As documented under Arrays:

    An array in PHP is actually an ordered map. A map is a type that associates values to keys.

    [ deletia ]

    An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.

    array(
        key  => value,
        key2 => value2,
        key3 => value3,
        ...
    )
    
  2. How to access an element in this array.

    As documented under Accessing array elements with square bracket syntax:

    Array elements can be accessed using the array[key] syntax.

    Example #6 Accessing array elements

    <?php
    $array = array(
        "foo" => "bar",
        42    => 24,
        "multi" => array(
             "dimensional" => array(
                 "array" => "foo"
             )
        )
    );
    
    var_dump($array["foo"]);
    var_dump($array[42]);
    var_dump($array["multi"]["dimensional"]["array"]);
    ?>
    

    The above example will output:

    string(3) "bar"
    int(24)
    string(3) "foo"
    
eggyal
  • 122,705
  • 18
  • 212
  • 237