0

PHP でこのオブジェクトに文字列を追加するにはどうすればよいですか? 文字列を生成することはできましたが、オブジェクトに文字列を追加したり、最後のコンマを削除したりすることはできません。

このような:

前、

{
    "themes":[
    {
        "name": "Amelia",
        "description": "Sweet and cheery.",
        "thumbnail": "http://bootswatch.com/amelia/thumbnail.png"
        }
    ]
}

後、

{
    "themes":[
            {
        "name": "juan",
        "description": "esto es un ejemplo.",
        "thumbnail": "http://example.com"
        },
                {
        "name": "juan2",
        "description": "esto es un ejemplo2.",
        "thumbnail": "http://example2.com"
        },
    ]
}

更新コード:



    // $_POST = Request::post
    if (Request::post('theme_title')) $theme_title = Request::post('theme_title'); else $theme_title = '';
    if (Request::post('theme_img')) $theme_img  = Request::post('theme_img'); else $theme_img = '';
    if (Request::post('theme_desc')) $theme_desc = Request::post('theme_desc'); else $theme_desc = '';

    $jsonfile = 'events.json';

    $data->themes[] = (object)array(
        "name"        => $theme_title,
        "description" => $theme_img,
        "thumbnail"   => $theme_desc

    );

    $json = str_replace('\/','/',json_encode( $data ));

    // $_POST = Request::post
    if (Request::post('Themes_add')){

          // file_put_contents
         File::setContent( $jsonfile, $json,$create_file = true, $append = true, );
    }

4

2 に答える 2

1

これは単なる辞書オブジェクトなので、次のことができます。

obj["key1"] = [];
obj["key2"] = [];

{
    "themes":[
            {
        "name": "juan",
        "description": "esto es un ejemplo.",
        "thumbnail": "http://example.com"
        },
                {
        "name": "juan2",
        "description": "esto es un ejemplo2.",
        "thumbnail": "http://example2.com"
        },
    ],
    "key1":[],
    "key2":[]
}

または、$ personに保存されている別のオブジェクトをテーマキーに追加するには、次のようにします。

obj["themes"][] = $person;
于 2012-12-07T23:01:15.357 に答える
0

「juan2」、それは面白い。このようなもの:

$obj->themes[] = (object)array(
    "name"        => "juan2",
    "description" => "esto es un ejemplo2.",
    "thumbnail"   => "http://example2.com"
);

乾杯

于 2012-12-07T23:02:14.327 に答える