Reputation: 3
Thanks in advance for any assistance you could offer.
I have a string like this:
[{"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"}}]
I am not sure how I could put it into an array so that I could utilise all of the Attachment ID's?
Something like:
echo $attachment_id[0];
echo $attachment_id[1];
etc.
Upvotes: 0
Views: 119
Reputation: 4963
It's a JSON encoded string. Just decode it into and array with json_decode http://php.net/manual/en/function.json-decode.php and get what you need.
<?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)
Upvotes: 0
Reputation: 517
This is not a string. Put it in quotes, then it is a string, more precisely it is JSON . You can use json_decode to parse it into an array.
$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'];
Upvotes: 0
Reputation: 10469
That "string" you have is in JSON format, PHP has some functions built in to use it!
You can either turn it into an object or an array with json_decode
Here's an example to turn it into a PHP array:
<?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);
The output of which is:
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
)
)
)
Upvotes: 1
Reputation: 2540
use json_decode() like below. you will get an array in result.
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"}}])
Upvotes: -1
Reputation: 4305
$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);'
Upvotes: 0
Reputation: 50858
That is JSON.
PHP has a built in JSON parser you can use:
$attachments = json_decode($data, true);
foreach ($attachments as $att) {
echo $att["title"] . "\n";
}
Upvotes: 1