2017-04-02 92 views
0

我在那個對象中有一個數組對象galleryImages我有像json字符串,在這個galleryImages我想要顯示第一個圖像值,如何才能獲得唯一的第一個圖像值?如何獲得數組對象的第0個索引鍵值

下面

$data=Array 
(
    [dbResultgallery] => Array 
     (
      [0] => stdClass Object 
       (
        [gallery_id] => 1 
        [className] => 1 
        [sectionName] => 1 
        [title] => Title 1 
        [description] => gfhfg 
        [galleryImages] => ["1.jpeg","2.jpg"] 
        [reg_on] => 2017-04-03 12:21:59 
        [created_by] => [email protected] 
        [school_id] => 2 
        [status] => 0 
       ) 

      [1] => stdClass Object 
       (
        [gallery_id] => 2 
        [className] => 1 
        [sectionName] => 1 
        [title] => Title 2 
        [description] => sdfdsfsdfsdfsdf 
        [galleryImages] => ["3.jpeg","4.jpg","5.jpg"] 
        [reg_on] => 2017-04-03 12:23:37 
        [created_by] => [email protected] 
        [school_id] => 2 
        [status] => 0 
       ) 

     ) 

) 

我現在用json_decode我的陣列檢查,看我下面的代碼

foreach ($dbResultgallery as $gal) : 
    $role = json_decode($gal->galleryImages,true); 
endforeach; 

現在我得到陣列格式像

Array 
    (
     [0] => 1.jpeg 
     [1] => 2.jpg 
    ) 

    Array 
    (
     [0] => 3.jpeg 
     [1] => 4.jpg 
     [2] => 5.jpg 
    ) 

預期結果

我只需要在第一圖像值等1.jpeg和3.jpeg,每個陣列我只需要第0項值

更新代碼

var_dump(json_decode($ gal-> galleryImages,true ))

array(2) { 

[0]=> 
    string(37) "6be1954c4bec91fe26fb7447fc551782.jpeg" 
    [1]=> 
    string(36) "153651d989591e76444c92cf037d5ac4.jpg" 
} 

array(3) { 
    [0]=> 
    string(37) "f356ceafa408e61cd1c62cfc39752b32.jpeg" 
    [1]=> 
    string(36) "a4fd29005696d92e0fc4cd3931454609.jpg" 
    [2]=> 
    string(36) "8eaaecdac1ff219192806acba7978a1b.jpg" 
} 

回答

1

改成這樣:

$role = json_decode($gal->galleryImages,true)[0]; 
+0

我試過,但沒有什麼是顯示 –

+0

現在越來越像回答第一個字符串,這意味着我的圖片值one.jpeg三,JPG意味着我得到的答案像o和t –

+0

@subikshanM好吧,你用'var_dump(json_decode($ gal-> galleryImages,true))得到了什麼? – Schlaus

0
foreach ($dbResultgallery as $gal) : 
     if(strpos(json_decode($gal->galleryImages,true) ,"jpeg")!== false){ 
     echo 'found'; // do your stuff here 
     $role = json_decode($gal->galleryImages[0],true); 
     } 
    endforeach 
+0

沒有基於擴展我寫的代碼,對我來說它不適合,我需要第一個索引值 –

+0

你能請告訴我什麼是解決方案 –

相關問題