使用PHP在数组中搜索和返回键

时间:2017-09-22 12:28:49

标签: php arrays

我有一个数组如下

[1428997804] => False
[1428997830] => False
[1428997921] => False
[1428997947] => True
[1428998025] => False
[1428998051] => False
[1428998077] => False
[1428998116] => False
[1428998142] => False
[1428998272] => False
[1428998389] => False
[1428998415] => False
[1428998467] => False
[1428998493] => False
[1428998519] => False
[1428998532] => False
[1428998623] => False
[1428998649] => False
[1428998675] => False

和UI(前端表单)有一个“从”开始作为文本字段,“结束”作为带有提交按钮的文本字段。用户在“start from”中输入上述数组中的值为1428998116,将“end to”输入为1428998467.现在我需要返回一个数组,该数组介于这些范围之间,其值与(True / False)对应。

我希望输出如下:

[1428998116] => False
[1428998142] => False
[1428998272] => False
[1428998389] => False
[1428998415] => False
[1428998467] => False

我不知道如何实现它。我想通过array_search来做,但似乎没有在正确的页面上。

任何人都可以指导我完成它。

3 个答案:

答案 0 :(得分:1)

您需要的只是一个循环,从$start_from开始到$end_to,构建一个新数组。

详细了解next()current()reset()key()

<?php
$array = array(10 => 1,
               20 => 2,
               30 => 3,
               40 => 4,
               50 => 5,
               60 => 6,
               70 => 7,
               80 => 8,
               90 => 9);
// start and end keys of the sub-array you want to extract
$start_from = 20;
$end_to = 50;
// Advance the internal pointer of the array
// until you rach the $start_from key
while (key($array) !== $start_from) {
    next($array);
}

// Now build the sub array
$slice = array();
while (key($array) <= $end_to) {
    $slice[key($array)] = current($array);
    next($array);
}

// Reset the internal array pointer
// just in case you need to loop again
// in the future
reset($array);

?>

最后$slice将是:

Array
(
    [20] => 2
    [30] => 3
    [40] => 4
    [50] => 5
)

答案 1 :(得分:1)

您可以使用array_filter

点击此处的工作演示https://eval.in/866420

$array = [];
$array[1428997804] = False;
$array[1428997830] = False;
$array[1428997921] = False;
$array[1428997947] = True;
$array[1428998025] = False;
$array[1428998051] = False;
$array[1428998077] = False;
$array[1428998116] = False;
$array[1428998142] = False;
$array[1428998272] = False;
$array[1428998389] = False;
$array[1428998415] = False;
$array[1428998467] = False;
$array[1428998493] = False;
$array[1428998519] = False;
$array[1428998532] = False;
$array[1428998623] = False;
$array[1428998649] = False;
$array[1428998675] = False;
$startFrom = 1428998116;
$endTo = 1428998467;

$slice = array_filter($array, function($k) use ($startFrom, $endTo) {
    return ($k >= $startFrom && $k <= $endTo) ? true : false ;
}, ARRAY_FILTER_USE_KEY);

echo "<pre>";
var_dump($slice);

答案 2 :(得分:0)

如果start和end在数组中有相应的键,那么很容易:

function findPos($keyNeedle, $array) {
    // array_search returns the key of the element given by the first Argument. Because of calling findPos with $keys as second argument it returns the index of the key we're looking for.
    return array_search($keyNeedle, $array);
}
// create an Array with all the keys and numbered index
$keys = array_keys($array);
// $start is the index of the given key inside of the keys array
$start = findPos(1428998116, $keys);
// $end is the index of the given key inside of the keys array
$end = findPos(1428998467, $keys);
var_dump(
    array_slice($array, $start, $end-$start+1, true); // true = preserve keys!
);

编辑:删除了array_combine和一个array_slice,因为在使用array_slice的第四个参数时不需要它

相关问题