如何将此自定义字符串解析/拆分为数组?

时间:2013-02-04 10:10:41

标签: php string

提前感谢您提供的任何帮助。

我有一个这样的字符串:

[{"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];

6 个答案:

答案 0 :(得分:1)

那是JSON。

PHP内置JSON parser你可以使用:

$attachments = json_decode($data, true);
foreach ($attachments as $att) {
    echo $att["title"] . "\n";
}

答案 1 :(得分: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
                )

        )

)

答案 2 :(得分: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);'

答案 3 :(得分: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'];

答案 4 :(得分:0)

这是一个JSON编码的字符串。只需使用json_decode http://php.net/manual/en/function.json-decode.php将其解码为数组,然后获取所需内容即可。

<?php
$string = 'the string above';

$array = json_decode($string, true);
$attachments = array_map(function($e) {
    return $e['image']['attachment_id']; 
}, $array);
print_r($attachments); // prints Array (114, 222, 333)

答案 5 :(得分:-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"}}])