Home > Back-end >  Remove one element from array php
Remove one element from array php

Time:10-22

Please I'm trying to remove 1670937087 from this array but the output I'm getting is not what I want.

Here is my code

<?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);

?>

My code give me this output

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

But I want something like this

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

CodePudding user response:

You need to store $fields back into $done, and encode that.

There's no need to call array_flip() to convert back to the array, use 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);
  • Related