0

I'm sending post array from swift to php server. Some issues my array looks like the server:

$my_array = $_POST['array'];

$my_array is ["item1","item2","item3"] but type is string not array.

I want to get array elements but when I try $my_array[0] is returning the first element of string " as you know.

I know I need to change my swift code but I wanna learn how can I convert these string to array and get elements.

Thanks

3
  • That line of code has invalid syntax. I suppose you need to add some escape characters... "But type is string": then please use string-literal notation to reflect exactly what it is. Commented Feb 25, 2020 at 16:30
  • 1
    If fixed, that string looks like JSON. In that case you can do $my_array = json_decode($str);, where $str is your string. Commented Feb 25, 2020 at 16:32
  • 1
    Ohh. Absolutely. Thanks @trincot. My mind is freeze :) Commented Feb 25, 2020 at 16:34

1 Answer 1

1

Your $_POST['array'] looks like JSON encoded.

If so, you can get the array with:

$my_array = json_decode($_POST['array']);
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.