1

我是 PHP 的新手,所以如果这太简单和明显,我很抱歉。

我有一个网络表单,用户可以动态添加输入字段。当用户单击提交时,所有输入字段都会保存到一个数组中。当我使用 print_r 命令时,我得到一个如下所示的数组:

Array 
    ( 
    [1] => Array ( [tc] => 00:10 [feedback] => good ) 
    [2] => Array ( [tc] => 00:20 [feedback] => bad ) 
    [3] => Array ( [tc] => 00:21 [feedback] => Bigger text ) 
    )

我用来将数据拉入数组的代码是:

if (!empty($_POST)) { //The values have been posted
    $elements = array();
    foreach ($_POST as $key => $val) { //For every posted values
        $frags = explode("_", $key); //we separate the attribute name from the number
        $id = $frags[1]; //That is the id
        $attr = $frags[0]; //And that is the attribute name
        if (!empty($val)) {
            //We then store the value of this attribute for this element.
            $elements[$id][$attr] = htmlentities($val);
        }
    }
}

我想显示数据的方式是这样的:

01: 00:10 - good<br />
02: 00:20 - bad<br />
03: 00:21 - Bigger text<br />

我正在尝试制作的表格可以在http://ridefreemedia.com.au/test找到

4

4 回答 4

1

您必须迭代数组,如下所示:

$arr = Array( 
    1 => Array ( "tc" => "00:10", "feedback" => "good" ),
    2 => Array ( "tc" => "00:20", "feedback"=> "bad" ),
    3 => Array ( "tc" => "00:21", "feedback" => "Bigger text" ) 
  );

foreach ($arr as $k => $info) {
  echo $k . ': ' . $info['tc'] . ' - ' . $info['feedback'] . '<br />'; 
}

这里的输出将是:

1: 00:10 - 好
2: 00:20 - 坏
3: 00:21 - 更大的文字

2 值得注意的事情,我相信你会发现PHP

  • .连接字符串
  • foreach ($array as $k => $v)遍历数组。
    对于每一行,它将行的键放在 中$k,将值放在 中$v。在这里,$k = 1$info = Array ( "tc" => "00:10", "feedback" => "good" )第一次迭代中。
于 2013-11-11T23:34:59.743 回答
0

print_r won't do, as you can see it outputs the meta data of the array. try adding the field to a string and use echo to output the strings into the html code.

于 2013-11-11T23:31:55.783 回答
0

只需像这样调用数组$array_name[1]。或者,如果数组值有一个键,$array_name['key_name'].

于 2013-11-11T23:29:29.960 回答
0

这不完全是你所追求的,但<pre>会美化一点。

echo '<pre>';
print_r($val);
echo '</pre>';
于 2013-11-11T23:29:59.950 回答