2013-02-04 57 views
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]; 

+1

嘗試'json_decode' – ogzd

+0

在此帖子中的最高回答json_decode()。我也用過這個。所以爲什麼我的回答是低估的,我不明白。如果有人解釋說會感謝我。謝謝。現在我明白了爲什麼它被低估了。無需回覆。謝謝。 – ripa

回答

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 
       ) 

     ) 

) 
1

這就是JSON。

PHP有一個內置的JSON parser你可以使用:

$attachments = json_decode($data, true); 
foreach ($attachments as $att) { 
    echo $att["title"] . "\n"; 
} 
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);' 
+0

我用我的小眼睛窺探,開始的東西:解析錯誤。 – Dale

-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"}}])

+0

你實際上會得到一個解析錯誤。即使它有效,它也會是一個對象,而不是數組。 – Dale

+0

好的。感謝@Dale – ripa

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']; 
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)