在PHP中显示数组内容后跟一个字符串

时间:2012-10-22 01:44:30

标签: php

这是我的代码结构的简洁。我需要做的是打印复选框的值(仅当用户检查了这些值时)以及作为消息附加的字符串。本质上,消息应该像'删除以下? 10,20,30'

通过我的编码,我得到类似Delete following?Array ( [0] => 15 [1] => 35 )

的内容

有人可以帮我解决这个问题。非常感谢。 (注意:并未提及所有适当的代码都应该在<html></html>下,并提供与理解相关的内容(我相信已足够)

<?php
if(isset($_POST['submit']))
{
echo "Remove following?";
print_r ($_POST['del']);
}
?>

<html>
<form>
// inside html in a dynamically created table I have the following
<?php
echo "<td class='tbldecor1'><input type='checkbox' name='del[]' value='$cxVal'></td>";
echo "<td class='tbldecor2' style='text-align:center'>".trim($rec["firstrow"])."</td>";
<?
<div><input type="submit" name="deleterec" id="deleterec" value="Delete"></div>
</form>
</html>

编辑:我也在想,如果我要使用循环打印,如何通过用户选择的复选框值替换1,2,3,4,5值?我想的循环如下;

 <?php 
 $a = array(1, 2, 3, 4, 5); 
 foreach ($a as $b) 
 { 
 print $b . " "; 
 } 
 ?> 

3 个答案:

答案 0 :(得分:2)

您可以在implode

的帮助下完成此操作
$vals = $_POST['del'];
echo "Remove following? " . implode( ", ", $vals );

更新(在@Waleed Khan关于HTML注入的警告之后):

// Make sure everything is a number (non-numeric will be replaced with boolean falses)
$vals = filter_var_array( $_POST['del'], FILTER_VALIDATE_INT ); 
// Remove all boolean falses
$vals = array_filter( $vals );
// String output
$valString = implode( ", ", $vals );
echo "Remove following? {$valString}";

答案 1 :(得分:0)

而不是print_r,它打印出有关变量的可读信息,使用implode命令:

echo "Delete following? " . implode(", ",$_POST['del']);

答案 2 :(得分:0)

您可能需要查看implode()

您的整个for循环可以替换为:

echo implode(" ", $a);