0

関数でそれを行ったので、セッションの設定解除に問題があり、内部パラメーターのみが選択値 ID を削除します。

見てください:

/***
 * @name DeleteItem
 * @date 04.10.2014
 * @param   The array that contains the element to delete
 * @param   The id of the selected index
 * @param   The name of the button that start the delete
 * @version 1.0
 * @description Delete an item to the select
 */

function DeleteItem($array,$SelectID,$btnDelete) {
    //The delete button was clicked?
    if(isset($_POST[$btnDelete]))
    {
    //Find the element with the id and delete the value
    foreach ($array as $idx =>$value)
    {
        if ($idx == $SelectID)
        {
             unset($array,$SelectID);
        }
    }
    return $array;
    } 

ありがとう - 本当に簡単なことだと思います。

4

3 に答える 3

1

実行しようunset()としている操作の種類に対して構文が間違っています。$SelectID配列からインデックスのみを削除したい。以下のコードを試してください:

unset($array[$SelectID]);

また、ループは必要ありません。以下は簡略化されたバージョンです。

function DeleteItem($array,$SelectID,$btnDelete) {
   //The delete button was clicked? and if index exists in array
   if(isset($_POST[$btnDelete]) && array_key_exists($SelectID, $array)) {
         unset($array[$SelectID]);
   }
   return $array;
}

DeleteItem()また、 POST 変数が存在する場合にのみ削除 (call ) する必要があります。したがって、以下のようにさらに単純化して、条件isset($_POST[$btnDelete])から削除できます。if

if(isset($_POST[$btnDelete])) {
   DeleteItem($array,$SelectID);
}
于 2013-10-06T13:03:10.040 に答える
0

最後に見つかったのは、参照値の問題だけでした(私はコピーで作業していました)関数の前に & を追加するだけです

于 2013-10-08T13:54:14.687 に答える
0

「 unset」を間違って使用しています。

$SelectIDが配列の値の場合、

$index = array_search($SelectID, $array);
if ($index) {
  unset($array[$index]);
}

または、$SelectIDが配列の「キー」であり、値ではない場合...

$keys = array_keys($array);
$index = array_search($SelectID, $keys);
if (!empty($keys[$index])) {
   unset($array[$keys[$index]]);
}
print_r($array);

(Pss: foreach は必要ありません)

于 2013-10-06T13:20:14.490 に答える