2013-03-11 137 views
2

我在Joomla數據庫中獲得一些信息,我需要循環並提取某些信息。從數組中獲取信息PHP

我可以成功地循環通過文章,但我不能從[圖像]獲取信息。如果可能的話,我只需將「image_intro」:「image/banner_box.jpg」拉到「banner_box.jpg」。

我的代碼到目前爲止循環通過數據庫。

foreach ($result as $item) { 
        //makes array 
        $newsitems[] = array(
         'title' => $item->title, 
         'text' => $item->introtext, 
         'image' => $item->images 

        );     
       } 

我出來打印。

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

) 

謝謝大家。

回答

3

$用品 - >圖像是JSON格式,所以你需要將其解碼在感謝

foreach ($result as $item) { 
    $imgData = json_decode($item->images, true); 

    // create array 
    $newsitems[] = array(
     'title' => $item->title, 
     'text' => $item->introtext, 
     'image' => $imgData['image_intro'] 
    );     
} 
+1

現貨:d – Brent 2013-03-11 11:47:54

2

該數據以json格式進行編碼。您需要使用json_decode()進行解碼。你的代碼會是這個樣子(你的循環內):

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

然後,您可以像這樣的對象的訪問屬性:$images_data->images_intro

2

嘗試json_decode(),如:

$decoded = json_decode($newsitems[0]['image'], true); //make it array for later access