0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

多次元配列(多次元連想配列)をJSONに変換した際に想定外の変換がされる

Last updated at Posted at 2024-08-30

事象

Ajax経由でテーブルから取得した多次元連想配列をJSONに変換したが、想定外の形式になってしまった。

変換する配列

default_flgの設定値を基準に、グループ化してから変換したい。

Array
(
    [0] => Array
        (
            [id_name] => aaa
            [content] => aaaaaaaaaaaaaa
            [default_flg] => 0
        )

    [1] => Array
        (
            [id_name] => bbb
            [content] => bbbbbbbbbbbb
            [default_flg] => 1
        )

    [2] => Array
        (
            [id_name] => ccc
            [content] => cccccccccccccc
            [default_flg] => 0
        )

    [3] => Array
        (
            [id_name] => ddd
            [content] => dddddddddddd
            [default_flg] => 1
        )

)

実行するソースコード

$defaultContents = array_filter($contentArray, function ($array){
    $default_flg = 1;
    return $array['default_flg'] == $default_flg;
});

$Contents = array_filter($contentArray, function ($array){
    $default_flg = 0;
    return $array['default_flg'] == $default_flg;
});

return json_encode([$defaultContents, $Contents]);

変換したい形式

[
    [
        {"id_name":"bbb","content":"bbbbbbbbbbbb","default_flg":"1"},
        {"id_name":"ddd","content":"dddddddddddd","default_flg":"1"}
    ],
    [
        {"id_name":"aaa","content":"aaaaaaaaaaaaaa","default_flg":"0"},
        {"id_name":"ccc","content":"cccccccccccccc","default_flg":"0"}
    ]
]

結果

違う、そうじゃない。

[
    {
        "1":{"id_name":"bbb","content":"bbbbbbbbbbbb","default_flg":"1"},
        "3":{"id_name":"ddd","content":"dddddddddddd","default_flg":"1"}
    },
    {
        "0":{"id_name":"aaa","content":"aaaaaaaaaaaaaa","default_flg":"0"},
        "2":{"id_name":"ccc","content":"cccccccccccccc","default_flg":"0"}
    }
]

原因

変換元の配列のindexが"0"から始まっていない。

$defaultContentsの配列

Array
(
    [1] => Array
        (
            [id_name] => bbb
            [content] => bbbbbbbbbbbb
            [default_flg] => 1
        )

    [3] => Array
        (
            [id_name] => ddd
            [content] => dddddddddddd
            [default_flg] => 1
        )

)

解決策

array_valuesを使用して、indexが"0"から始まるようにした。
array_values=元のindexを無視して値を順に設定していく関数とのこと。

$defaultContents = array_filter($contentArray, function ($array){
    $default_flg = 1;
    return $array['default_flg'] == $default_flg;
});

$Contents = array_filter($contentArray, function ($array){
    $default_flg = 0;
    return $array['default_flg'] == $default_flg;
});

return json_encode([array_values($defaultContents), array_values($Contents)]);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?