I have some information in a Joomla Database I need to loop through, and pull out certain information.
I can successfully loop through articles fine, but I cant get the information of from the [image]. I need to pull just the "image_intro":"image/banner_box.jpg" if possible just the "banner_box.jpg".
My code so far to loop through database.
foreach ($result as $item) {
//makes array
$newsitems[] = array(
'title' => $item->title,
'text' => $item->introtext,
'image' => $item->images
);
}
My out put printed.
Array
(
[0] => Array
(
[title] => Service 2
[text] => <p>Lorem ipsum dolor sit amet, conseteetur sadipscing elitr, sed diam monumy eirmod..<a href="http://www.google.co.uk">View more</a></p>
[image] => {"image_intro":"images\/banner_box2.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
)
[1] => Array
(
[title] => Service 1
[text] => <p>Lorem ipsum dolor sit amet, conseteetur sadipscing elitr, sed diam monumy eirmod..<a href="http://www.google.co.uk">View more</a></p>
[image] => {"image_intro":"images\/banner_box1.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
)
)
Thanks everyone.
$item->images is in json format, so you need to decode it
foreach ($result as $item) {
$imgData = json_decode($item->images, true);
// create array
$newsitems[] = array(
'title' => $item->title,
'text' => $item->introtext,
'image' => $imgData['image_intro']
);
}
That data is encoded in json
format. You need to decode it using json_decode(). You code will look something like this (inside your loop):
$images_data = json_decode($item->images);
You can then access properties of the object like this: $images_data->images_intro
.
try with json_decode(), like:
$decoded = json_decode($newsitems[0]['image'], true); //make it array for later access