从函数构建数组

时间:2015-04-09 01:47:35

标签: php arrays foreach

我需要在网站的每个页面上运行以下功能:

<?php 
foreach ($page as $key => $item) 
if ($page[$key][4] == basename(__DIR__)) 
    echo $page[$key][0] . " " . $page[$key][1] . " " . $page[$key][2] . " " . $page[$key][3] . " " . $page[$key][4];
?>

我想检索数组中的字符串/变量/,以便以动态方式在页面中使用它。如下所示:

<?php 
$result = array(
'val0' => " ",      // Value from $page[$key][0]
'val1' => " ",      // Value from $page[$key][1]
'val2' => " ",      // Value from $page[$key][2]
'val3' => " ",      // Value from $page[$key][3]
'val4' => " "       // Value from $page[$key][4]
);
?>

我到处寻找这样的东西。怎么办?

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

$result = array();
foreach ($page as $key => $item) {
    if ($page[$key][4] == basename(__DIR__)) {
        foreach ($page[$key] as $val) {
            $result[] = $val;
        }
        break;
    }
}

演示:http://codepad.org/xM4ZCIrP