数组在for循环中不重复

时间:2014-11-07 11:55:45

标签: php mysql arrays for-loop

我有一个自动填充多选表单的数组。我必须显示一些值作为初始值,我有一个数组。 这是我的代码。

 for($i=0; $i<count($temp_multy_name); $i++)
 {

     echo  $temp_multy_name[$i]; echo"&nbsp";
     $pac->set_init_selection(array(
        array("id"=>"$temp_multy_name[$i]", "text"=>"$temp_multy_name[$i]"),
     ));

 } 

当我运行echo $temp_multy_name[$i]时,我得到了php和mysql,但是当我在set_init_selection中应用它时,我只得到了最后一个值,不知道为什么。谁能帮帮我吗?


enter image description here

3 个答案:

答案 0 :(得分:2)

这是您的解决方案

  $setInitSelection = array();
  for($i=0; $i<count($temp_multy_name); $i++)
  {

     echo  $temp_multy_name[$i]; echo"&nbsp";
     $setInitSelection[] = array("id"=>$temp_multy_name[$i], "text"=>$temp_multy_name[$i]);

  } 
  $pac->set_init_selection($setInitSelection);

答案 1 :(得分:1)

也许你正在覆盖你的阵列。试试这个:

$selection_array = array();

for($i=0; $i<count($temp_multy_name); $i++)
{
    //echo  $temp_multy_name[$i]; echo"&nbsp";
    $selection_array[] = array("id"=>$temp_multy_name[$i], "text"=>$temp_multy_name[$i]);
} 

$pac->set_init_selection($selection_array);

答案 2 :(得分:1)

多次调用$ pac-&gt; set_init_name(array $ val),覆盖以前的值。试试这个:

$initialSelection = array();
for($i=0; $i<count($temp_multy_name); $i++)
  {
     echo  $temp_multy_name[$i]; echo"&nbsp";
     $initialSelection[$i] = array("id"=>$temp_multy_name[$i], "text"=>$temp_multy_name[$i]);
  } 

  $pac->set_init_selection(array($initialSelection);

希望有所帮助。

相关问题