2

我想从不同的循环中获取一些 id 并在另一个页面上内爆 theidès,但我每次只从循环中获取最后一个 id。

这里是第一页的代码 - 我的表格:

    while($rowSCA = mysql_fetch_array($ResCSCA)) {
    $libSCA =   $rowSCA['lib_service'];
    $idSCA =   $rowSCA['id_service'];
        echo '<tr>';
        echo "<td class='HSCA'>".$libSCA."</td><td class='HSCA'><input type='checkbox' class='HSCA' name=\"centre[".$idSCA."]\"/></td>";  
        echo '</tr>';
        echo '<input type="hidden" name="SCA['.$idSCA.']" value="'.$idSCA.'"></input>';   
        }    
    ...
    while($rowSRiA = mysql_fetch_array($ResCSRiA)) {

    $libSRiA =   $rowSRiA['lib_service'];
    $idSRiA =    $rowSRiA['id_service'];


        echo "<tr class='HSRiA'>";
        echo "<td class='HSRiA'>".$libSRiA."</td><td class='HSRiA'><input type='checkbox' class='HSRiA' name=\"centre[".$idSRiA."]\"/></td>";  
        echo '</tr>';
        echo '<input type="hidden" name="RiA['.$idSRiA.']" value="'.$idSRiA.'"></input>'; 
        }

这是我的内爆第二页的代码:

    // First implode operation
    foreach($_POST['SCA'] as $Cservices) 
        {
            @$CALLservices = array($Cservices);
        }

        $IMPCServices = implode(",", $CALLservices);

    $CNewServices = $IMPCServices;

    // Second implode operation
    foreach($_POST['RiA'] as $Cservices2) 
        {
            @$CALLservices2 = array($Cservices2);
        }

        $IMPCServices2 = implode(",", $CALLservices2);

    $CNewServices2 = $IMPCServices2;

    echo $CNewServices;
    echo '<br>';
    echo $CNewServices2;

我必须正常得到这个结果集:

    94,100,101,104
    93

但我只得到

    104
    93

但是我的 var_dump 仍然告诉我,我得到了我所有的 id:

    array(4) {
      [94]=>
      string(2) "94"
      [100]=>
      string(3) "100"
      [101]=>
      string(3) "101"
      [104]=>
      string(3) "104"
    }
    array(1) {
      [93]=>
      string(2) "93"
    }

那么为什么内爆不能正常工作呢?

有人有想法吗?

提前 THX

4

3 回答 3

2

当你这样做时:

foreach($_POST['SCA'] as $Cservices) 
    {
        @$CALLservices = array($Cservices);
    }

每次通过循环时,您都会$CALLservices用仅包含循环当前元素的数组覆盖变量。如果你想创建一个包含所有元素的数组,你应该这样做:

$CALLservices = array()
foreach($_POST['SCA'] as $Cservices) 
    {
        $CALLservices[] = array($Cservices);
    }

但是,您根本不需要这个循环。你可以写:

$CALLservices = $_POST['SCA']

复制数组。为什么你甚至需要复制数组?你可以写:

$IMPCServices = implode(",", $_POST['SCA']);
于 2013-05-07T09:31:09.620 回答
1

您以错误的方式添加到数组中试试这个

    $CALLservices2 = array();
    $i = 0;
    foreach($_POST['SCA'] as $Cservices) 
    {
         $CALLservices2[$i] =  $Cservices;
         $i++;
    }

现在做内爆

   $IMPCServices2 = implode(",", $CALLservices2);
于 2013-05-07T09:28:35.470 回答
1

foreach循环中,您不会获取数组中的所有值。

有不同的方法,但这里是一个基本的方法:

foreach($_POST['SCA'] as $Cservices) {
        @$CALLservices[] = $Cservices;
}

$IMPCServices = implode(",", $CALLservices);

享受....

于 2013-05-07T09:32:49.737 回答