在PHP中foreach的语法 [英] syntax for foreach in php

查看:287
本文介绍了在PHP中foreach的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个语法在php中是什么意思?
$ b $ p $ foreach $ meta $ t = $ $ data $ >



上面的$ t => $ data是什么意思。



因为foreach基本上是(例如来自w3school)

 < ?php 
$ colors = array(red,green,blue,yellow);
foreach($ colors为$ value)
{
echo$ value< br>;
}
?>

上述情况下,$ value代表 $ t => $ data



$ colors 表示 $ meta code $
$ b $数组元$如下所示
$ b $ pre $ $ $ $ c > $ 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',

);

那么$ t是什么,$ data是什么

如果我想在'脸谱'中获得'标题'作为一个单独的关键如何做到这一点。即。将

  $ t => $ data => $ final work 
$ b $ t = facebook或twitter
$ data = title等
$ final = og:title等


解决方案

阅读 http://www.php.net/manual/en/control-structures.foreach.php ,关注示例。

  / * foreach示例3:键和值* / 

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

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

/ * foreach示例4:多维数组* /
$ a = array();
$ a [0] [0] =a;
$ a [0] [1] =b;
$ a [1] [0] =y;
$ a [1] [1] =z;
$ b foreach($ a as $ v1){
foreach($ v1 as $ v2){
echo$ v2\\\
;






如果你使用php编码阅读官方手册不是'all-around-sites'。

已添加

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

foreach($ a as $ k => $ v){
echo$ k => $ v;
//会显示
// one => 1two => 2three => 3
}

foreach($ a as $ v){
echo$ v;
//会显示
// 12317

code $ $ $ $ $ $ $ $ $ $ $ $ ,如果我们使用$ k(在我们的foreach())中有'key'的名字。 $ v

pre pre $ p $数组(
'title',&b; $ b'b'='>'$' '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',

);
$ b foreach($ meta as $ sKey => $ aValue){
// $ sKey =='facebook'
// $ aValue = array()$ b $ ($ aValue as $ sKeyInner => $ sValue){
// $ sKeyInner =='$ b $ title'
// $ sValue =='og:title'
}
}

就这样,我放弃了。 XD


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

解决方案

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

这篇关于在PHP中foreach的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆