我是 php 新手,所以如果我要问一些基本的东西,请原谅我以前问过的东西。我已经用谷歌搜索了几天了,尤其是。这个论坛我找到了我以前的大部分答案,但我找不到任何关于这个问题的东西,所以我必须问。这是我的第一个问题。
// this array is coming from MySQL db as a result. It's a list of user's friends and it could contain dozens or hundreds of friends. Now he wants to put them in different groups.
$array = array(
array("John", "Doe", "1"),
array("Peter", "Citizen", "2")
...
);
// a page is created with the result. Each record has a checkbox that the user can select.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
foreach($array as $item){
$nItem = $item;
?>
<input type="text" name="fname" value="<?php if(isset($nItem)){echo $nItem[0];} ?>" readonly="readonly" />
<input type="text" name="lname" value="<?php if(isset($nItem)){echo $nItem[1];} ?>" readonly="readonly" />
<input type="text" name="uid" value="<?php if(isset($nItem)){echo $nItem[2];} ?>" readonly="readonly" />
<input type="checkbox" name="val[]" value="<?php if(isset($nItem)){echo $nItem;} ?>" /> // I want to send this $nItem array as it is to the action page and read its keys and values there.
<br>
<?php
}
?>
<input type="submit" name="submit" value="Submit">
</form>
<br />
<?php
// if I tick both checkboxes and submit I get the following:
if(isset($_POST['val'])){
$val = $_POST['val'];
echo var_dump($val), '<br />'; // array(2) { [0]=> string(5) "Array" [1]=> string(5) "Array" }
echo count($val), '<br />'; // 2
print_r($val); // Array ( [0] => Array [1] => Array )
echo("val is {$val[0]}"); // val is Array
foreach($val as $key => $value){
echo "Key and Value are: ".$key." ".$value, '<br />'; // Key and Value are: 0 Array
} // Key and Value are: 1 Array
}
?>
它只返回一个字符串“Array”,而不是我可以从中读取 velues 的实际数组。如果我将单个值放入复选框值中,例如 -
if(isset($nItem)){echo "".$nItem[0]." ".$nItem[1]." ".$nItem[2]."";}
——然后我得到——
0 John Doe 1
——但这不是我想要的。我想要我可以遍历的操作页面上的实际数组。我认为既然它已经是一个数组,那并不难,但我错了。
谁能告诉我怎么做?
提前谢谢了。