what does this syntax means in php
foreach ( $meta as $t => $data )
what does $t => $data mean in the above.
because foreach is basically (example from w3school)
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value)
{
echo "$value <br>";
}
?>
it the above case $value represents $t => $data
$colors
represents $meta
the array $meta is as follows
$meta = Array(
'facebook' => Array(
'title' => 'og:title',
'type' => 'og:type',
'url' => 'og:url',
'thumbnail' => 'og:image',
'width' => 'og:image:width',
'height' => 'og:image:height',
'sitename' => 'og:site_name',
'key' => 'fb:admins',
'description' => 'og:description'
),
'twitter' => Array(
'card' => 'twitter:card',
'description' => 'twitter:description',
)
);
then what is $t and what is $data
also if i want to get 'title' in 'facebook' as a seprate key how to do it. ie. will
$t => $data => $final work
$t = facebook or twitter
$data = title etc
$final = og:title etc
In your example it means $meta is an array, and $t is the array keys and $data is the value of the keys. For example, $t could be 3 and $data "yellow"
Read this http://www.php.net/manual/en/control-structures.foreach.php , attention on examples.
PS: If you coding using php read official manual not 'all-around-sites'.
ADDED
So, if we use $k (in our foreach()) we have 'key' name. In $v we always have 'value'.
ADDED
That's all, I give up. XD
If you use
foreach
it loops over array's items. Base syntax is:you can use extended syntax
which allows you to get item's key in
$key
variable. For example:$array
does not contain keys, so in$key
variable you have numbers (starts with0
).If keys are defined (associative arrays),
$key
will take defined key value:$t => $data means
$t - Index (key)
$data - value
This syntax allows you to iterate over all items and also have the key of the array handy.
Taking a look at the documentation, this syntax is explained best as follows:
In your example, the key will be the elements index within the array.
There are two types of arrays in php
For normal array:
For Associative array:
Eg : Array ( [0] => Array ( [Bookcode] => 124 [Bookname] => asd ) [1] => Array ( [Bookcode] => 125 [Bookname] => asd ) )