-2

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":[]}

1 Answers1

0

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Sir your code give me this output `{"done":{"0":5415703999,"2":6887688688},"skip":[],"left":[]}` but I want something like this `{"done":[5415703999,6887688688],"skip":[],"left":[]}` – JOBIANS TECHIE Oct 21 '22 at 20:32
  • When array indexes aren't sequential, `json_encode()` creates an object instead of array. I've solved the problem by calling `array_keys()` instead of `array_flip()`. – Barmar Oct 21 '22 at 20:35
  • [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Oct 22 '22 at 23:54
  • I am convinced that 1. You are a more knowledgeable developer than I am and 2. You are a more experienced stack overflow contributor than I am. This is why I am always surprised when you don't close closable pages. I don't dv your posts because that would make no impact at all. – mickmackusa Oct 23 '22 at 02:12