1

I have some json data which i have posted from a from but can't process it and able to store in database, here is what data posted looks like

Array
(
    [apd0] => [{"ratio":"1","size":"S","quantity":"83"},{"ratio":"2","size":"M","quantity":"166"}]
    [apd1] => [{"ratio":"1","size":"S","quantity":"83"},{"ratio":"2","size":"M","quantity":"166"},{"ratio":"3","size":"N","quantity":"200"}]
)

is there any way i can change it into associative array and will i be able to classify their name.

I have already tried

$data = json_decode($your_json_string, TRUE);
5
  • What output you want from that json? Commented Nov 15, 2016 at 8:04
  • Associative array Commented Nov 15, 2016 at 8:04
  • u already have Associative array Commented Nov 15, 2016 at 8:07
  • print_r(json_decode('[{"ratio":"1","size":"S","quantity":"83"},{"ratio":"2","size":"M","quantity":"166"}]', TRUE)); This return you associative array what is wron with that? Commented Nov 15, 2016 at 8:09
  • @HuzoorBux Json data is in a array, it will give an error Commented Nov 15, 2016 at 8:16

1 Answer 1

3

i think you can it like this

$test = Array
    (
        'apd0' => '[{"ratio":"1","size":"S","quantity":"83"},{"ratio":"2","size":"M","quantity":"166"}]',
        'apd1' => '[{"ratio":"1","size":"S","quantity":"83"},{"ratio":"2","size":"M","quantity":"166"},{"ratio":"3","size":"N","quantity":"200"}]'
    );

    foreach($test as $key =>$val)
    {
        $array[$key] = json_decode($val, true);
    }

output:

Array
(
[apd0] => Array
    (
        [0] => Array
            (
                [ratio] => 1
                [size] => S
                [quantity] => 83
            )

        [1] => Array
            (
                [ratio] => 2
                [size] => M
                [quantity] => 166
            )

    )

[apd1] => Array
    (
        [0] => Array
            (
                [ratio] => 1
                [size] => S
                [quantity] => 83
            )

        [1] => Array
            (
                [ratio] => 2
                [size] => M
                [quantity] => 166
            )

        [2] => Array
            (
                [ratio] => 3
                [size] => N
                [quantity] => 200
            )

    )

)

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.