我如何获得使用foreach的多维数组值? [英] how can i get multidimensional array values using foreach?

查看:172
本文介绍了我如何获得使用foreach的多维数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不同的函数中使用多维数组,所以我把它作为一个全局变量(数组).i创建了一个多维数组,并将其作为全局访问不同的函数。现在我怎样才能得到值从它使用foreach循环?
这里是我的代码

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ array $ =v1,
input2=>v2),
数组(
input3=>v3,
input4 >v4)
);

类测试
{
函数testp()
{
global $ test;
foreach($ test as $ key => $ value)
{
echo $ value;
}
var_dump($ test);
echo is_array($ test);
}
}

$ obj = new testing();
$ obj-> testp();

我用is_array和 var_dump 来确认是否它的一个数组。
都罚款
,并忽略错误抑制忽略。现在我怎样才能从它的价值?

解决方案

它是数组的数组, ($ test $ key => $ value)


$ b $ $ ($ value为$ k => $ v){
echo $ v;




$ p
$ b $ p $这会回应你的价值v1,v2,v3, v4一个接一个。


I want to use a multidimensional array in different functions.so i am making it as a global variable(array).i created a multidimensional array and made it as global to access in different function.now how can i get the values from it using foreach loop? here is my code

$test=array(
       array(
        "input1"=>"v1",
        "input2"=>"v2"),
        array(
         "input3"=>"v3",
         "input4"=>"v4")
      );

class testing
{
  function testp()
  {
    global $test;
    foreach($test as $key => $value)
    {
      echo $value;
    }
    var_dump($test);
    echo is_array($test);
  }
}

$obj = new testing();
$obj->testp();

i used is_array and var_dump to confirm whether its an array. all are fine and its shwoing Error suppression ignored. now how can i get the values from it?

解决方案

It is array of arrays, what works for top order array, works further as well:

foreach($test as $key => $value)
{
   foreach($value as $k => $v){
      echo $v;
   }
}

This will echo your values v1, v2, v3, v4 one after another.

这篇关于我如何获得使用foreach的多维数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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