从数组php中删除一个元素

k2fxgqgv  于 2022-10-22  发布在  PHP
关注(0)|答案(1)|浏览(132)

请我尝试从这个数组中删除1670937087,但我得到的输出不是我想要的。
这是我的密码

<?php 
include "db_connect.php";
$sql2 = "SELECT * FROM ads WHERE campaign_id=132";
$res2 = mysqli_fetch_assoc(mysqli_query($conn,$sql2));
$done = json_decode($res2["done_by"]); //$done output= {"done":[5415703999,1670937087,6887688688],"skip":[],"left":[]}

$fields = array_flip($done->done);
unset($fields['1670937087']);
$fields = array_flip($fields);

$json = json_encode($fields);
print($json);

?>

我的代码给出了这个输出

{"0":5415703999,"2":6887688688}

但我想要这样的东西

{"done":[5415703999,6887688688],"skip":[],"left":[]}
kxe2p93d

kxe2p93d1#

您需要将$fields存储回$done,并对其进行编码。
不需要调用array_flip()来转换回数组,使用array_keys()

$done = json_decode($res2["done_by"]); //$done output= {"done":[5415703999,1670937087,6887688688],"skip":[],"left":[]}

$fields = array_flip($done->done);
unset($fields['1670937087']);
$done->done = array_keys($fields);
$json = json_encode($done);
print($json);

相关问题