【问题标题】:Getting elements in an array?获取数组中的元素?
【发布时间】:2014-03-04 20:29:49
【问题描述】:

我有一个数组:

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

我希望传递一封信,并获取信的两边。例如。 'f' 将是 'e' 和 'g'。

有没有简单的方法来做到这一点。

另外,如果我要选择“a”,我会希望响应为 null 和“b”。

这是我的实际数组,数组搜索如何处理多维数组?

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...

【问题讨论】:

  • 搜索字母,取它的索引,取index-1和index+1的元素,如果其中一个是count(array)返回null。
  • 谢谢 Merguez,只是想知道是否有一个 php 函数。
  • 您可以结合使用array_search(返回您搜索到的字母的索引)和简单的数组操作。

标签: php arrays function search information-retrieval


【解决方案1】:

数组搜索是一种可能性,如前所述,或者您可以通过 for 循环运行

var selection = 'a'; // your selection

var arr = [  'a', 'b', 'c', 'd', 'e', 'f']; // small array

var i = 0, // define loop i
    le = arr.length, // define loop length
    out, // output var
    match = ''; // matching data
    before = 'non', // before data 
    after = 'non'; // after data

for(i; i < le; i++){ // run the loop
  if(arr[i] === selection){ // if loop matches selection
    match = arr[i]; // set match var
    after = arr[i+1] || after; // set after var, if undifined, set "non" from above
    before = arr[i-1] || before; // set before var if undefined, set "non" from above
  }
}

out = 'match: ' + match + ' before: ' + before + ' after: ' + after; //build output

alert(out); 

http://jsbin.com/qixu/3/

【讨论】:

    【解决方案2】:

    你可以使用array_search()

    在数组中搜索给定的值,如果成功则返回相应的键

    <?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';
    

    Demo

    【讨论】:

    • 但是边缘情况呢。字母 a 的 $key-1 将是 -1,这是一个无效的密钥,对于 j 的 $key+1 也是如此
    • 处理边缘情况使用:echo isset($arr[$key-1]) ? $arr[$key-1] : null;
    • 谢谢,我的数组实际上是多维的,数组搜索如何处理它?更新了原始问题
    【解决方案3】:
    <?php
    $arr=array("a","b","c","d","e","f","g","h","i","j");
    
    function context($array,$element) {
      return array(@$array[array_search($element,$array)-1],@$array[array_search($element,$array)+1]);;
    }
    
    print_r(context($arr,"a"));
    print_r(context($arr,"e"));
    print_r(context($arr,"j"));
    

    输出:

    Array
    (
        [0] => 
        [1] => b
    )
    Array
    (
        [0] => d
        [1] => f
    )
    Array
    (
        [0] => i
        [1] => 
    )
    

    【讨论】:

      【解决方案4】:

      这行得通 -

      function find_neighbour($arr, $value){
          $index = array_search($value, $arr);
          if($index === false){
              return false;
          }
          if($index == 0){
              return Array(null, $arr[1]);
          }
          if($index == count($arr)-1){
              return Array($arr[$index-1], null);
          }
          return Array($arr[$index-1], $arr[$index+1]);
      }
      $a = Array('a','b','c','d','e','f','g','h','i','j');
      print_r(find_neighbour($a,"a"));    //[null, 'b']
      print_r(find_neighbour($a,"j"));    //['i', null]
      print_r(find_neighbour($a,"e"));    //['d', 'f']
      

      【讨论】:

        猜你喜欢
        • 2014-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-12
        • 2019-12-17
        • 1970-01-01
        相关资源
        最近更新 更多