Php 如何删除特定的数组块?
好的。让我们看看我是否能正确地解释我自己。在数据库的两行中,我有如下数据:Php 如何删除特定的数组块?,php,mysqli,Php,Mysqli,好的。让我们看看我是否能正确地解释我自己。在数据库的两行中,我有如下数据: row1 (labeled "claimed") = Item1, Item2, Item3, Item4 row2 (labeled ctype) = Thing1, Thing2, Thing3, Thing4 row1 (labeled "claimed") = Item1, Item3, Item4 row2 (labeled ctype) = Thing1, Thing3, Thing4 --------
row1 (labeled "claimed") = Item1, Item2, Item3, Item4
row2 (labeled ctype) = Thing1, Thing2, Thing3, Thing4
row1 (labeled "claimed") = Item1, Item3, Item4
row2 (labeled ctype) = Thing1, Thing3, Thing4
-------------------------------------------------------------------------
| Name | ctype | claimed |
--------------------------------------------------------------------------
| Jhon | Thing1, Thing2, Thing3, Thing4 | Item1, Item2, Item3, Item4 |
-------------------------------------------------------------------------
我已经调出了数据,所以他们像这样分组:
Item1, Thing1
Item2, Thing2...
使用此代码:
<?php
$select=mysqli_query($connect, "SELECT * FROM `$table_members` WHERE email='$_SESSION[USR_LOGIN]'");
while($row1=mysqli_fetch_assoc($select)) {
$name1=$row1['name'];
$claim = $row1['claimed'];
$type1 = $row1['ctype'];
if ($claim != 'None') {
$type2 = explode(', ', $type1);
$decks = explode(', ', $claim);
echo "<table id=\"memtable\"><tr><th>Name</th><th>Claimed</th><th>Type</th></tr>";
foreach(array_combine($decks, $type2) as $deck => $type3){
echo "<tr><td>";
echo "$name1</td><td>";
echo "$deck</td><td>";
echo "$type3</td><td>";
}
}
}
?>
他们选择Item2并提交表单。现在我只想从我的数据库中删除Item2和Thing2,所以现在这些行看起来像这样:
row1 (labeled "claimed") = Item1, Item2, Item3, Item4
row2 (labeled ctype) = Thing1, Thing2, Thing3, Thing4
row1 (labeled "claimed") = Item1, Item3, Item4
row2 (labeled ctype) = Thing1, Thing3, Thing4
-------------------------------------------------------------------------
| Name | ctype | claimed |
--------------------------------------------------------------------------
| Jhon | Thing1, Thing2, Thing3, Thing4 | Item1, Item2, Item3, Item4 |
-------------------------------------------------------------------------
我似乎不知道如何对其进行编码,以便只删除一个特定实例。
我假设是这样的:
foreach(array_combine($decks, $type2) as $deck => $type3){
$gone = "DELETE FROM `$table_members` WHERE claimed='$deck' AND ctype='$type3'";
mysqli_query($connect, $gone);
}
埃塔:我找到了解决办法!对于任何寻找同一类型事物的人,以下是我所做的:
$remove = $_POST['remove;']
$select1=mysqli_query($connect, "SELECT * FROM `$table_members` WHERE email='$_SESSION[USR_LOGIN]'");
while($row1=mysqli_fetch_assoc($select1)) {
$claim = $row1['claimed'];
$type1 = $row1['ctype'];
}
$type2 = explode(', ', $type1);
$decks = explode(', ', $claim);
for($i=0;$i<count($decks);$i++){
if($decks[$i]===$remove){unset($decks[$i]); unset($type2[$i]);}
}
$decks = implode(', ',$decks);
$type2 = implode(', ',$type2);
$gone = "UPDATE `$table_members` set claimed='$decks', ctype='$type2' where email='$_SESSION[USR_LOGIN]'";
mysqli_query($connect, $gone);
$remove=$\u POST['remove;']
$select1=mysqli\u查询($connect,“从“$table\u members`WHERE email=”$\u会话[USR\u登录]”)中选择*;
而($row1=mysqli\u fetch\u assoc($select1)){
$claim=$row1['claims'];
$type1=$row1['ctype'];
}
$type2=分解(“,”,$type1);
$decks=爆炸(“,”,$claim);
对于($i=0;$i让我们看看我是否正确理解您的场景
表中的数据如下所示:
row1 (labeled "claimed") = Item1, Item2, Item3, Item4
row2 (labeled ctype) = Thing1, Thing2, Thing3, Thing4
row1 (labeled "claimed") = Item1, Item3, Item4
row2 (labeled ctype) = Thing1, Thing3, Thing4
-------------------------------------------------------------------------
| Name | ctype | claimed |
--------------------------------------------------------------------------
| Jhon | Thing1, Thing2, Thing3, Thing4 | Item1, Item2, Item3, Item4 |
-------------------------------------------------------------------------
在这种情况下,如果您一次只想删除一项,我认为您最好的选择是:
$email = $_SESSION['USR_LOGIN'];
foreach(array_combine($decks, $type2) as $deck => $type3)
{
$gone = "UPDATE `$table_members` set claimed=REPLACE(claimed,'$deck, ',''), ctype=REPLACE(ctype,'$type3, ', '') where email='$email'";
mysqli_query($connect, $gone);
}
如果您可以发布一个示例,说明数据在表中的组织方式,我想我可以提供一个更好的示例。您确实需要规范化数据。抱歉,我不知道如何解释它。这正是它在表中的组织方式。我以前从未使用过替换,因此这对我来说是一个新领域。这几乎奏效了,但当我试图删除第二个条目,但只删除了第一个和第二个条目。我们就快到了!事实上,再看一遍,除了最后一个条目外,它删除了所有条目。