1

So here is my array:

array(1) { [0]=> string(79) "{"form_array":{"element1":"value1","element2":"value2","element3":"value3"}}" }

How can I extract "form_array" as an array?

4
  • That is not an array, that is a string. Commented Oct 9, 2012 at 9:52
  • Looks like json in a array to me @Mihai Iorga Commented Oct 9, 2012 at 9:54
  • 2
    weird, it doesn't to me :|, today I'm kinda .. blind. Late for work, tripped on stairs, dropped my coffee. Commented Oct 9, 2012 at 9:55
  • @Mihai Iorga Looking at it again ... that does not look like print_r or var_dump output ..... where did you get hat pt0 Commented Oct 9, 2012 at 10:08

3 Answers 3

2

You actually have just a simple array with one element, which is a string, that seems to be JSON encoded. To get its data, you may use the following:

// get the string
$data = $array[0];

// decode the content
$data = json_decode( $data, true );

// get the sub array
$data = $data['form_array'];

The steps may of course be simplified into a single row. I just separated them for readability and clarity.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, that was the json in an array. Thanks Sirko, I just altered for other people having the same. $data = json_decode($data, TRUE);
1

You can try

$array = json_decode($array[0]);
$formArray = $array->form_array;

Comments

1

If you have posted the result of var_dump(arrayname), then this code only will help you

$data = $data['form_array'];

else you can go with Sirko's anser

1 Comment

I guess you should take the first index of the array like Sirko did.

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.