Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove one element from array php

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

But I want something like this

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

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading