0

when run var_dump($myVariable) I have this string, this variable is not json :

string(13) "['1','2','3']"

but I want have this:

array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" }

how can I convert $myVariable to second format

7
  • 4
    Possible duplicate of Handling data in a PHP JSON Object Commented Sep 22, 2017 at 14:22
  • 1
    try json_decode on the string variable Commented Sep 22, 2017 at 14:23
  • 1
    @Calimero no that not json,that is array created in foreach but when export it, its return string inseted array Commented Sep 22, 2017 at 14:28
  • @Shahrukh have you seen any {} in my result!! thats not json Commented Sep 22, 2017 at 14:30
  • an you show us what is in your foreach please ? Commented Sep 22, 2017 at 14:36

1 Answer 1

2

If the string you are converting is consistent, the following isn't tested, but you can do something like the following:

$string = "['1','2','3']";
$string = str_replace(['[','\'',']'], '', $string);
$array = explode(',', $string);
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.