1

I have the variable that holds numbers...before writing it to database it must be in this format:

["1", "6", "11", "15"]

I got this from my ajax call:

1,6,11,15

I try to convert string with numbers to array...and now one way is to foreach to read from array and add to beginning and ending string " but i think that is bad idea...could be simpler way to archive this above output?

$numbers = '1,6,11,15';

$a = str_split($numbers);

print_r($a);
2
  • Possible XY Problem. How's that data being generated / sent? Commented Jan 9, 2018 at 10:06
  • your required format is json. you should use json_encode with explode with comma function Commented Jan 9, 2018 at 10:07

1 Answer 1

1
$numbers = '1,6,11,15';

$a = explode(",",$numbers);

echo json_encode($a);
Sign up to request clarification or add additional context in comments.

Comments

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.