dtfpznrbn503027700 2014-02-07 12:16
浏览 39
已采纳

获取数组中的元素?

I have an array:

array(a,b,c,d,e,f,g,h,i,j);

I wish to pass in a letter and get the letters either side of it. eg. 'f' would be 'e' and 'g'.

Is there an easy way to do this.

Also if I were to select 'a' I would want a response null and 'b'.

Here's my actual array, how would array search work with multidimensional array?

array(19) { [0]=> array(3) { ["id"]=> string(2) "46" ["title"]=> string(7) "A" ["thumb"]=> string(68) "013de1e6ab2bfb5bf9fa7de648028a4aefea0ade816b935dd423ed1ce15818ba.jpg" } [1]=> array(3) { ["id"]=> string(2) "47" ["title"]=> string(7) "B" ["thumb"]=> string(68) "9df2be62d615f8a6ae9b7de36671c9907e4dadd3d9c3c5db1e21ac815cf098e6.jpg" } [2]=> array(3) { ["id"]=> string(2) "49" ["title"]=> string(6) "Look 7" ["thumb"]=> string(68) "0bfb2a6dd1142699ac113e4184364bdf5229517d98d0a428b62f6a72c8288dac.jpg" } etc etc...
  • 写回答

4条回答 默认 最新

  • 普通网友 2014-02-07 12:19
    关注

    You could make use of array_search()

    Searches the array for a given value and returns the corresponding key if successful

    <?php
    $arr=array('a','b','c','d','e','f','g','h','i','j');
    $key = array_search('a',$arr);
    echo isset($arr[$key-1])?$arr[$key-1]:'NULL';
    echo isset($arr[$key+1])?$arr[$key+1]:'NULL';
    

    <kbd>Demo</kbd>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?