【问题标题】:With PHP, how do I get the element one before the one I know the key for使用 PHP,我如何在我知道关键的元素之前获取元素
【发布时间】:2020-12-24 23:10:41
【问题描述】:

想象一下在 PHP 脚本中设置的一些数组,如下所示:

 $foo = array(
     'bob'=>976,
     'Jack'=>'Octopus',
     'bar'=>'baz',
     'pi'=>'irrational',
     'other'=>123
 );
 
 $biz = array(
     'Harry'=>'Dogs',
     'Zim'=>'zoom',
     'bar'=>'baz',
 );

现在让我们说(出于某种原因)我想在“bar”之前获取数组第一个索引的内容。像这样:

 function get_before($ar,$what){
     // ... the bit I'm still working on
 }
 
 echo get_before($foo,'bar'); // should be Octopus
 echo get_before($biz,'bar'); // should be zoom

我认为我想要的是我面前的钥匙。有没有办法得到它? (您可以假设数组很少有 50 个元素那么大)。

我一直在玩弄使用array_keys,对新数组进行搜索,得到结果的索引值减去一来找到新键,然后使用该键来获取我的目标。这似乎是不必要的密集和hackish - 一个更简单的方法会让我更开心,因为我创建的函数会在每次页面加载时被调用很多。

【问题讨论】:

  • 使用您提供的名称搜索数组索引(例如'bar'),然后执行(减1)数组索引数据检索。
  • 如果顺序很重要,为什么要使用关联数组?
  • 我认为没有什么比您在上一段中描述的要好得多。没有任何内置功能可以做到这一点。

标签: php arrays


【解决方案1】:

您可以使用array_search 来查找键的索引。并使用索引 -1 找到前一个 kay=>value

       $array = array(
             'bob'=>976,
             'Jack'=>'Octopus',
             'bar'=>'baz',
             'pi'=>'irrational',
             'other'=>123
         );

function find_previous($array, $needle) {
    $keys  = array_keys($array);
    $index = array_search($needle, $keys);

    return $index ? $array[$keys[$index - 1]] : false;
}
var_dump(find_previous($array,'bar'));

【讨论】:

  • array_search() 是正确的答案
  • 如果您传递第一个键 - “bob”,可能会出错。
  • 我已经在描述中提到了
  • 如果给定的键是第一个,或者没有找到键。您可以检查 $index 是否为假:3v4l.org/3In3u
【解决方案2】:

您可以遍历您的数组,并存储最后一个值并等到您有匹配的键:

<?php

$foo = array(
    'bob'   => 976,
    'Jack'  => 'Octopus',
    'bar'   => 'baz',
    'pi'    => 'irrational',
    'other' => 123
);


function get_value_before_given_key($array, $needle) {
    $last = $result = null;
    foreach($array as $key => $value) {
        if($needle === $key) {
            $result = $last;
            break;
        }
        $last = $value;
    }

    return $result;
}

var_dump(get_value_before_given_key($foo, 'pi'));
var_dump(get_value_before_given_key($foo, 'nokey'));

输出:

string(3) "baz"
NULL

【讨论】:

  • 如果 null 是数组中的一个值,请注意结果不明确。
【解决方案3】:

我不知道这个有什么用,但必须做一些简单的工作。

function getPreviousValue($array, $key) {
    if (isset($array[$key])) { // checking if the passed key is existing
        end($array); // set internal array pointer to the end of the array
        while (key($array) !== $key) { // looping through the array to set the internal pointer position to the passed key
            prev($array);
        }
        
        prev($array); // set the internal position to previous key
        
        return current($array); // return the current value
    }
    
    return null;
}

【讨论】:

  • 我不认为这有什么区别,但是为什么你从结尾而不是开头开始呢?
  • @Barmar 因为语义和更好的可读性。你想要以前的值,所以对我来说,如果你只使用 prev() 而不是 next() 然后 prev()
【解决方案4】:

假设...

  1. 你只想输出前一个数组项的key
  2. 如果找不到密钥,您想返回 FALSE
  3. 如果键是数组中的第一个键,您想返回 TRUE
    • 为了区别FALSE,其中键存在于数组中

主题数组

$array = [
    'bob'   => 976,
    'Jack'  => 'Octopus',
    'bar'   => 'baz',
    'pi'    => 'irrational',
    'other' => 123
];

使用 foreach

function get_previous_a($array, $needle)
{
    $last = TRUE;
    foreach($array as $key => $value) {
        if($needle === $key)
            return $last;
        else
            $last = $key;
    }
    return FALSE;
}


var_dump(
    get_previous_a($array, "pi"),
    get_previous_a($array, "other"),
    get_previous_a($array, "bob"),
    get_previous_a($array, 132145) 
);

/* Output:

string(3) "bar"
string(2) "pi"
TRUE
bool(false)

*/    

使用数组_*

function get_previous_b($array, $needle)
{
    $keys       = array_keys($array);
    $curr_index = array_search($needle, $keys);
    $key        = ( $curr_index > 0 ) ? $keys[$curr_index - 1] : TRUE;
    return ( $curr_index === FALSE ) ? FALSE : $key;
}

var_dump(
    get_previous_b($array, "pi"),
    get_previous_b($array, "other"),
    get_previous_b($array, "bob"),
    get_previous_b($array, 132145) 
);

/* Output:

string(3) "bar"
string(2) "pi"
TRUE
bool(false)

*/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 2011-06-15
    • 2012-01-02
    • 2012-11-01
    • 1970-01-01
    • 2013-08-03
    • 2011-05-13
    • 2015-08-22
    相关资源
    最近更新 更多