0

よろしくお願いいたします。

次のような文字列があります。

[{"title":"Image One","description":"Image One\r\nDescription","image":{"attachment_id":"111"}},{"title":"Image Two","description":"Image Two Description","image":{"attachment_id":"222"}},{"title":"Image Three","description":"Image Three\r\nDescription","image":{"attachment_id":"333"}}]

すべてのアタッチメント ID を利用できるように、それを配列に入れる方法がわかりません。

何かのようなもの:

echo $attachment_id[0];
echo $attachment_id[1];

4

6 に答える 6

1

それがJSONです。

PHP には、使用できるJSON パーサーが組み込まれています。

$attachments = json_decode($data, true);
foreach ($attachments as $att) {
    echo $att["title"] . "\n";
}
于 2013-02-04T10:13:04.113 に答える
1

あなたが持っているその「文字列」はJSON形式であり、PHPにはそれを使用するための関数が組み込まれています!

json_decodeを使用して、オブジェクトまたは配列に変換できます

これを PHP 配列に変換する例を次に示します。

<?php

    $json = '[{"title":"Image One","description":"Image One\r\nDescription","image":{"attachment_id":"111"}},{"title":"Image Two","description":"Image Two Description","image":{"attachment_id":"222"}},{"title":"Image Three","description":"Image Three\r\nDescription","image":{"attachment_id":"333"}}]';
    $data = json_decode($json, true);
    print_r($data);

その出力は次のとおりです。

Array
(
    [0] => Array
        (
            [title] => Image One
            [description] => Image One
Description
            [image] => Array
                (
                    [attachment_id] => 111
                )

        )

    [1] => Array
        (
            [title] => Image Two
            [description] => Image Two Description
            [image] => Array
                (
                    [attachment_id] => 222
                )

        )

    [2] => Array
        (
            [title] => Image Three
            [description] => Image Three
Description
            [image] => Array
                (
                    [attachment_id] => 333
                )

        )

)
于 2013-02-04T10:15:37.013 に答える
0

これは文字列ではありません。引用符で囲むと、文字列、より正確にはJSONになります。json_decodeを使用して、配列に解析できます。

$myString = '[{"title":"Image One","description":"Image One\r\nDescription","image":
   {"attachment_id":"111"}},{"title":"Image Two","description":"Image Two 
   Description","image":{"attachment_id":"222"}},{"title":"Image 
   Three","description":"Image Three\r\nDescription","image":{"attachment_id":"333"}}]';

$myArray = json_decode($myString, true);
var_dump($myArray[0]['image']['attachment_id'];
于 2013-02-04T10:18:35.297 に答える
0

$encoded-variable = [{"title":"Image One","description":"Image One\r\nDescription","image":{"attachment_id":"111"}},{"title":"Image Two","description":"Image Two Description","image":{"attachment_id":"222"}},{"title":"Image Three","description":"Image Three\r\nDescription","image":{"attachment_id":"333"}}]

 use     `$result = json_decode($encoded_varaible,true);'
于 2013-02-04T10:13:41.593 に答える
-1

以下のように json_decode() を使用します。結果として配列が得られます。 json_decode([{"title":"Image One","description":"Image One\r\nDescription","image":{"attachment_id":"111"}},{"title":"Image Two","description":"Image Two Description","image":{"attachment_id":"222"}},{"title":"Image Three","description":"Image Three\r\nDescription","image":{"attachment_id":"333"}}])

于 2013-02-04T10:15:36.553 に答える