How can I access an array/object?



PHP Snippet 1:

echo $array[0];

PHP Snippet 2:

echo $array{0};

PHP Snippet 3:

//Declaring an array
$arrayA = array ( /*Some stuff in here*/ );
$arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4

//Accessing an array element
echo $array[0];

PHP Snippet 4:

echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
         // ???????????????  ????????????????  ??????????????????????????????
         // ?                ?                 ??? 3rd Array dimension;
         // ?                ????????????????????? 2d  Array dimension;
         // ?????????????????????????????????????? 1st Array dimension;

PHP Snippet 5:

echo $object->property;

PHP Snippet 6:

echo $objectA->objectB->property;

PHP Snippet 7:

 //Both methods/styles work and access the same data
 echo $object->anotherObject->propertyArray["elementOneWithAnObject"]->property;
 echo $object->
        anotherObject
        ->propertyArray
        ["elementOneWithAnObject"]->
        property;

 //Both methods/styles work and access the same data
 echo $array["arrayElement"]["anotherElement"]->object->property["element"];
 echo $array["arrayElement"]
     ["anotherElement"]->
         object
   ->property["element"];

PHP Snippet 8:

Array (
    [data] => Array (
            [0] => stdClass Object (
                    [propertyXY] => 1
                )    
            [1] => stdClass Object (
                    [propertyXY] => 2
                )   
            [2] => stdClass Object (
                    [propertyXY] => 3                   
               )    
        )
)

PHP Snippet 9:

foreach($array as $key => $value)

PHP Snippet 10:

Array (  //Key: array
    [0] => stdClass Object (
            [propertyXY] => 1
        )
    [1] => stdClass Object (
            [propertyXY] => 2
        )
    [2] => stdClass Object (
            [propertyXY] => 3
        )
)

PHP Snippet 11:

foreach($array["data"] as $key => $value)

PHP Snippet 12:

stdClass Object (  //Key: 0
    [propertyXY] => 1
)
stdClass Object (  //Key: 1
    [propertyXY] => 2
)
stdClass Object (  //Key: 2
    [propertyXY] => 3
)

PHP Snippet 13:

$array = [
    "key" => (object) [
        "property" => [1,2,3]
    ]
];

PHP Snippet 14:

array(1) {
  ["key"]=>
  object(stdClass)#1 (1) {
    ["property"]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
    }
  }
}

PHP Snippet 15:

Array
(
    [key] => stdClass Object
        (
            [property] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )

        )

)

PHP Snippet 16:

array (
  'key' => 
  (object) array(
     'property' => 
    array (
      0 => 1,
      1 => 2,
      2 => 3,
    ),
  ),
)

PHP Snippet 17:

// var_dump()
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)    // <-- value we want to access
  [2]=>
  int(3)
}

// print_r()
Array
(
    [0] => 1
    [1] => 2    // <-- value we want to access
    [2] => 3
)

// var_export()
array (
  0 => 1,
  1 => 2,    // <-- value we want to access
  2 => 3,
)

PHP Snippet 18:

// var_dump()
object(stdClass)#1 (1) {
  ["property"]=>
  /* Array here */
}

// print_r()
stdClass Object
(
    [property] => /* Array here */
)

// var_export()
(object) array(
    'property' => 
  /* Array here */
),

PHP Snippet 19:

// var_dump()
array(1) {
  ["key"]=>
  /* Object & Array here */
}

// print_r()
Array
(
    [key] => stdClass Object
        /* Object & Array here */
)

// var_export()
array (
  'key' => 
  /* Object & Array here */
)

PHP Snippet 20:

echo $array["key"]->property[1];

PHP Snippet 21:

2

PHP Snippet 22:

Array ( [key] => HERE ) 

PHP Snippet 23:

echo $arr["key"];

PHP Snippet 24:

array(1) {
    ["</b>
key"]=>
    string(4) "HERE"
}

PHP Snippet 25:

echo $arr["</b>\nkey"];

PHP Snippet 26:

HERE

PHP Snippet 27:

<?xml version="1.0" encoding="UTF-8" ?> 
<rss> 
    <item> 
        <title attribute="xy" ab="xy">test</title> 
    </item> 
</rss>

PHP Snippet 28:

SimpleXMLElement Object
(
    [item] => SimpleXMLElement Object
    (
        [title] => test
    )

)

PHP Snippet 29:

echo $xml->asXML();  //And look into the source code

highlight_string($xml->asXML());

header ("Content-Type:text/xml");
echo $xml->asXML();

PHP Snippet 30:

<?xml version="1.0" encoding="UTF-8"?>
<rss> 
    <item> 
        <title attribute="xy" ab="xy">test</title> 
    </item> 
</rss>

PHP Snippet 31:

echo array_values($get_user)[0]; // 10499478683521864 

PHP Snippet 32:

    Array ( [demo] => Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => [email protected] ) )

PHP Snippet 33:

    Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => [email protected] )

PHP Snippet 34:

[
    'id' => 10499478683521864,
    'birthday' => '07/22/1983',
    'email' => '[email protected]',
    'first_name' => 'Alan',
    'gender' => 'male',
    'last_name' => 'Malmsteen',
    'link' => 'https://www.facebook.com/app_scoped_user_id/1049213468352864/',
    'location' => (object) [
        'id' => 102173722491792,
        'name' => 'Jakarta, Indonesia'
    ],
    'locale' => 'id_ID',
    'middle_name' => 'El-nino',
    'name' => 'Alan El-nino Malmsteen',
    'timezone' => 7,
    'updated_time' => '2015-05-28T04:09:50+0000',
    'verified' => 1
]

PHP Snippet 35:

foreach ($response as $key1 => $value1) {
    if (is_scalar($value1)) {
        echo "Key1: $key1, Value1: $value1\n";
    } else {
        foreach ($value1 as $key2 => $value2) {
            echo "Key1: $key1, Key2: $key2, Value2: $value2\n";
        }
    }
}

PHP Snippet 36:

Key1: id, Value1: 10499478683521864
Key1: birthday, Value1: 07/22/1983
Key1: email, Value1: [email protected]
Key1: first_name, Value1: Alan
Key1: gender, Value1: male
Key1: last_name, Value1: Malmsteen
Key1: link, Value1: https://www.facebook.com/app_scoped_user_id/1049213468352864/
Key1: location, Key2: id, Value2: 102173722491792
Key1: location, Key2: name, Value2: Jakarta, Indonesia
Key1: locale, Value1: id_ID
Key1: middle_name, Value1: El-nino
Key1: name, Value1: Alan El-nino Malmsteen
Key1: timezone, Value1: 7
Key1: updated_time, Value1: 2015-05-28T04:09:50+0000
Key1: verified, Value1: 1