访问php数组内部

时间:2014-10-29 14:40:37

标签: php arrays

我有这样的阵列打印:

array(2) {
  ["systems"]=>
  array(5) {
    [1]=>
    string(1) "1111"
    [2]=>
    string(1) "2222"
    [3]=>
    string(1) "3333"
    [4]=>
    string(1) "4444"
    [5]=>
    string(1) "5555"
  }
  ["test"]=>
  string(2) "on"
}

现在,我希望使用foreach或任何可以使用该代码的代码来访问每个内部值。

我的意思是,例如,我想回应这样的事情:

System "1" has "1111" value.
System "2" has "2222" value.
System "3" has "3333" value.
System "4" has "4444" value.
System "5" has "5555" value.

我如何拥有该输出代码?

3 个答案:

答案 0 :(得分:3)

试试这个:

$array = array(
    'systems' => array(
        1 => 1111,
        2 => 2222,
        3 => 3333,
        4 => 4444,
        5 => 5555
    ),
    'test' => 'on'
);

foreach ($array['systems'] as $key => $value) {
    echo 'System "' . $key . '" has "' . $value . '" value.'."<br />\n";
}

如果您的变量名称为$array

答案 1 :(得分:2)

如下。

foreach ($array as $key => $subarray){
    foreach ($subarray as $name => $value){
        echo '<p>'.$key.' "'.$name.'" has "'.$value.'" value.</p>';
    }
}

答案 2 :(得分:0)

 foreach ($array['systems'] as $key1 => $value1) {
         echo "SYSTEM".$key1."has".$value1."value";
         echo "<br>";

     }