首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从数组PHP中抓取信息

从数组PHP中抓取信息
EN

Stack Overflow用户
提问于 2013-03-11 19:37:36
回答 3查看 120关注 0票数 2

我需要遍历Joomla数据库中的一些信息,并从中提取某些信息。

我可以成功地循环通过文章很好,但我不能从图像中获得信息。我只需要拉出"image_intro":"image/banner_box.jpg“,如果可能的话,只拉"banner_box.jpg”。

到目前为止,我的代码循环通过数据库。

代码语言:javascript
复制
foreach ($result as $item) {
                    //makes array
                    $newsitems[] = array(
                        'title' => $item->title,
                        'text'  => $item->introtext,
                        'image' => $item->images

                    );                  
                }

我的输出打印出来了。

代码语言:javascript
复制
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":""}
        )

)

谢谢大家。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-11 19:40:52

$item->图像为json格式,因此需要对其进行解码

代码语言:javascript
复制
foreach ($result as $item) {
    $imgData = json_decode($item->images, true);

    // create array
    $newsitems[] = array(
        'title' => $item->title,
        'text'  => $item->introtext,
        'image' => $imgData['image_intro']
    );                  
}
票数 3
EN

Stack Overflow用户

发布于 2013-03-11 19:40:23

该数据以json格式编码。您需要使用json_decode()进行解码。你的代码看起来会像这样(在你的循环中):

$images_data = json_decode($item->images);

然后,您可以像这样访问对象的属性:$images_data->images_intro

票数 2
EN

Stack Overflow用户

发布于 2013-03-11 19:40:35

尝试使用json_decode(),例如:

代码语言:javascript
复制
$decoded = json_decode($newsitems[0]['image'], true); //make it array for later access
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15337581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档