2

First of all, I really don't know if that kind of topic exist. But I searched a lot and now I am here.

My question about parsing. For example I would like to unset some items.

$now = array();
$now[0]['name'] = "Hello1";
$now[0]['si'] = "BumBum1";
$now[1]['name'] = "Hello2";
$now[1]['si'] = "BumBum2";
$now[2]['name'] = "Hello3";
$now[2]['si'] = "BumBum3";
$now[3]['name'] = "Hello4";
$now[3]['si'] = "BumBum4";

echo json_encode($now)."<br>";

unset($now[0]);

echo json_encode($now);

And the output:

[{"name":"Hello1","si":"BumBum1"},{"name":"Hello2","si":"BumBum2"},{"name":"Hello3","si":"BumBum3"},{"name":"Hello4","si":"BumBum4"}]

{"1":{"name":"Hello2","si":"BumBum2"},"2":{"name":"Hello3","si":"BumBum3"},"3":{"name":"Hello4","si":"BumBum4"}}

And my the JSON file turns to messy code. Appears numbers and etc. Any ideas how to solve this.

1
  • Tried with objects. Same problem. Commented Nov 10, 2015 at 18:13

1 Answer 1

2

You need to "reindex" the array (use array_values() function).

//unset..
unset($now[0]);

//reindex
$now = array_values($now);

//display as before
echo json_encode($now);
Sign up to request clarification or add additional context in comments.

2 Comments

I just forgot that.. Thank you very much!
If this answer helped you: upvote, if answer is correct mark it as such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.