2017-04-02 190 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

你能请告诉我什么是解决方案 –

相关问题