-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":[]}
4
  • php.net/manual/en/function.array-values.php Commented Oct 21, 2022 at 20:20
  • CherryDT please I Don't understand anything from that website Commented Oct 21, 2022 at 20:22
  • From $done output which is {"done":[5415703999,1670937087,6887688688],"skip":[],"left":[]} Commented Oct 21, 2022 at 20:27
  • Implementation of the very easy-to-find accepted answer on the dupe target: 3v4l.org/9BeTP Commented Oct 22, 2022 at 23:52

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.