从数组中删除变量()

时间:2017-02-15 21:08:58

标签: php

我有一个包含这些内容的数组:' a'' b',' b',' c'。订单并不总是相同的,有时它甚至有不同的字符串。我该如何去除一个' b'从阵列?

澄清一下:我正在使用array_diff()从$ _SESSION [' cart']中删除一个字符串。但是如果两个字符串相同,它将删除两个字符串。我怎样才能删除一个

CART:

session_start();
ob_start();
  if (isset($_GET["delete"])) {
      ob_clean();
      echo "<div class='background' id='page1'>";
      $todelete = $_GET["delete"];
      $_SESSION['cart'] = array_diff($_SESSION['cart'], array($todelete));

      if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
                drawcart();
                drawclear();
      }
      else {
          drawempty();
      }
   }
   else {
       ob_clean();
       echo "<div class='background' id='page1'>";
       if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
           drawcart();
           drawclear();
       }
       else {
           drawempty();
       }
   }
   function drawcart() {
       foreach ($_SESSION['cart'] as $key => $value) {
           $unserializedItem = unserialize(base64_decode($value));
           echo "
                <div class='container no-shadow cartitem' id='" . $value . "'>
                    <div class='row'>
                        <div class='twelve columns'>
                            <h2 class='no-padding2'>Envelop " . $unserializedItem["envelop"] . "</h2>
                        </div>
                        <div class='end'></div>
                    </div>
                    <div class='row' style='height: 6.625rem'>
                        <div class='three columns' style='height:calc(100% - 1rem);''>
                            <img class='high' src='img/220x110logo_links.png' alt=''>
                        </div>
                        <div class='five columns'>
                            <p>Aantal:</p>
                            <p>Aantal kleuren:</p>
                            <p>Papierkleur:</p>
                            <p>Vensterpositie:</p>
                            <p>Klepsluiting:</p>
                        </div>
                        <div class='four columns'>
                            <p>" . $unserializedItem["aantal"] . "</p>
                            <p>" . $unserializedItem["kleur"] . "</p>
                            <p>" . $unserializedItem["papier"] . "</p>
                            <p>" . $unserializedItem["venster"] . "</p>
                            <p>" . $unserializedItem["lock"] . "</p>
                        </div>
                        <div class='end'></div>
                    </div>
                    <div class='row'>
                        <div class='twelve columns'>
                            <a class='linkbutton' href='?delete=" . $value . "'>Verwijder item</a>
                        </div>
                        <div class='end'></div>
                    </div>
                </div>";
        }
    }
    function drawclear() {
        echo "
            <div class='container no-shadow'>
                <div class='row'>
                    <div class='twelve columns'>
                        <a class='linkbutton' href='clearwinkelwagen.php'>Winkelwagen legen</a>
                    </div>
                    <div class='end'></div>
                </div>
            </div>";
    }
    function drawempty() {
        echo "<div class='container no-shadow'>
                   <div class='row'>
                        <div class='twelve columns'>
                            <h1 class='no-padding2'>Winkelwagen is leeg.</h1>
                            <a class='no-padding2 linkbutton' href='../kies2/#page2'>Voeg enveloppen toe.</a>
                        </div>
                        <div class='end'></div>
                    </div>
                </div>";
    }`

ADDWINKELWAGEN.PHP(您可以从表单页面发送到此处):

session_start();

$optionsSelected = array();

if (isset($_GET["envelop"])) {
$optionsSelected["envelop"] = $_GET["envelop"];
}
if (isset($_GET["aantal"])) {
$optionsSelected["aantal"] = rtrim(rtrim($_GET["aantal"], 'a'), 'b');
}
if (isset($_GET["kleur"])) {
$optionsSelected["kleur"] = $_GET["kleur"];
}
if (isset($_GET["papier"])) {
$optionsSelected["papier"] = $_GET["papier"];
}
if (isset($_GET["venster"])) {
$optionsSelected["venster"] = $_GET["venster"];
}
if (isset($_GET["lock"])) {
    $optionsSelected["lock"] = $_GET["lock"];
}


if (count($optionsSelected) < 6) {

}
else {
    $key = base64_encode(serialize($optionsSelected));
    $_SESSION['cart'][] = $key;
}

CLEARWINKELWAGEN.PHP:

session_start();

session_destroy();

2 个答案:

答案 0 :(得分:0)

使用unset删除它。您可以使用array_search来获取与搜索条件匹配的第一个元素的索引。

String myTxt = fromFileInJar("/com/com/thing/a.txt");

注意: 如果您的问题的目的是删除重复项,那么@Anant就会为您提供答案。 < / p>

答案 1 :(得分:0)

Synny Patel的答案得以解决。通过添加此代码来实现它:

$todelete = $_GET["delete"];
unset($_SESSION['cart'][array_search($todelete,$_SESSION['cart'])]);

而不是:

$_SESSION['cart'] = array_diff($_SESSION['cart'], array($todelete));

谢谢!