syntax for foreach in php

2019-09-21 11:37发布

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

标签: php foreach
6条回答
萌系小妹纸
2楼-- · 2019-09-21 12:17

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"

查看更多
\"骚年 ilove
3楼-- · 2019-09-21 12:21

Read this http://www.php.net/manual/en/control-structures.foreach.php , attention on examples.

/* foreach example 3: key and value */

$a = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
);

foreach ($a as $k => $v) {
    echo "\$a[$k] => $v.\n";
}

/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

PS: If you coding using php read official manual not 'all-around-sites'.

ADDED

$a = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
);

foreach ($a as $k => $v) {
    echo "$k => $v";
    // will show
    // one => 1two => 2three => 3
}

foreach ($a as $v) {
    echo "$v";
    // will show 
    // 12317
}

So, if we use $k (in our foreach()) we have 'key' name. In $v we always have 'value'.

ADDED

    $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',
        )
    );

    foreach ($meta as $sKey => $aValue) {
        // $sKey == 'facebook'
        // $aValue = array()
        // $aValue['title'] == 'og:title' 

        foreach ($aValue as $sKeyInner => $sValue) {
            // $sKeyInner == 'title'
            // $sValue == 'og:title'
        }
    }

That's all, I give up. XD

查看更多
地球回转人心会变
4楼-- · 2019-09-21 12:22

If you use foreach it loops over array's items. Base syntax is:

foreach ($array as $item) {
  // first loop:  $item=foo
  // second loop: $item=bar
}

you can use extended syntax

foreach ($array as $key => $item) {}

which allows you to get item's key in $key variable. For example:

$array = array('foo', 'bar');
foreach ($array as $key => $item) {
  // first loop:  $key=0, $item=foo
  // second loop: $key=1, $item=bar
}

$array does not contain keys, so in $key variable you have numbers (starts with 0).

If keys are defined (associative arrays), $key will take defined key value:

$array = array('key1' => 'foo', 'key2' => 'bar');
foreach ($array as $key => $item) {
  // first loop:  $key=key1, $item=foo
  // second loop: $key=key2, $item=bar
}
查看更多
别忘想泡老子
5楼-- · 2019-09-21 12:28

$t => $data means

$t - Index (key)

$data - value

查看更多
老娘就宠你
6楼-- · 2019-09-21 12:28

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:

foreach (array_expression as $key => $value)
    statement

...[this syntax] will additionally assign the current element's key to the $key variable on each iteration.

In your example, the key will be the elements index within the array.

查看更多
走好不送
7楼-- · 2019-09-21 12:40

There are two types of arrays in php

  1. Normal arrays
  2. Associative arrays

For normal array:

 <?php
      $colors = array("red","green","blue","yellow");
      foreach ($colors as $value)
        {
        echo "$value";   // prints  red,green...
        }
      ?> 

For Associative array:

Eg : Array ( [0] => Array ( [Bookcode] => 124 [Bookname] => asd ) [1] => Array ( [Bookcode] => 125 [Bookname] => asd ) )

<?php 
foreach($arr as $key => $val)
        {
           echo $key;             //prints--> 124
           echo $val;             //prints -->asd
        }

?>
查看更多
登录 后发表回答