2

My Array :

$val=array('a','b','c');
echo json_encode($val);

Output : ["a","b","c"]

Using unset to remove 'b' from location '1' : unset($val[1]);

Output:{"0":"a","2":"c"}

Expecting Output as : ["a","c"]

Is there any solution ? to get rid of keys and curl brackets and get output as expected!

2 Answers2

1

You need to reset array keys. Check this:

<?php

$val=array('a','b','c');
echo json_encode($val);

unset($val[1]);

echo json_encode($val); //outputs {"0":"a","2":"c"}


$val = array_values($val); //reset array keys
echo json_encode($val); //outputs ["a","c"]
aslawin
  • 1,981
  • 16
  • 22
0

Use array_splice() array_splice($val, 1, 1); instead of unset($val[1]);

$val=array('a','b','c');
array_splice($val, 1, 1);
echo json_encode($val);

Test Here.

Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58