-4

I have following JSON data received.

{
    "app": "000",
    "members":[
        {
            "name":{
                "value": "PIYO"
            },
            "email":{
                "value": "piyo@alk.jp"
            }
        },
        {
            "name":{
                "value": "HOGE"
            },
            "email":{
                "value": "hoge@alk.jp"
            }
        }
    ]
}

I would like to convert it to following array.

[["name" => "PIYO", "email" => "piyo@alk.jp"], ["name" => "HOGE", "email" => "hoge@alk.jp"]]

How can I handle it? Please give me some advice.

tajihiro
  • 2,293
  • 7
  • 39
  • 60

3 Answers3

1

You could use

$array = json_decode($json, true);

Don't forget that $json must be in string.

I tried to test result on PHP.

<?php
$json = '{
    "app": "000",
    "members":[
        {
            "name":{
                "value": "PIYO"
            },
            "email":{
                "value": "piyo@alk.jp"
            }
        },
        {
            "name":{
                "value": "HOGE"
            },
            "email":{
                "value": "hoge@alk.jp"
            }
        }
    ]
}';

$array = json_decode($json, true);

print_r($array['members']);

Result:

Array
(
    [0] => Array
        (
            [name] => Array
                (
                    [value] => PIYO
                )

            [email] => Array
                (
                    [value] => piyo@alk.jp
                )

        )

    [1] => Array
        (
            [name] => Array
                (
                    [value] => HOGE
                )

            [email] => Array
                (
                    [value] => hoge@alk.jp
                )

        )

)
Thân LƯƠNG Đình
  • 3,082
  • 2
  • 11
  • 21
0
$phparray = json_decode($json, true);
janw
  • 8,758
  • 11
  • 40
  • 62
Matas Lesinskas
  • 414
  • 6
  • 13
  • Your answer would be better if you explained how and why this affects the problem at hand. Simply dumping code is less likely to help the OP and any inexperienced future readers as well. @Danial No, it wouldn't. The second parameter (`true`) specifies that the JSON should be converted as an associative array. – El_Vanja Dec 11 '20 at 15:44
-2
$foo = json_decode($json,true);
$members = $foo['members'];

$members will be the array you want.

Danial
  • 1,496
  • 14
  • 15