- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
"RegisterControl-6ren">
我想从 $routes 数组中获取匹配的路由。如果有多个数组具有相同的“ur”值。我想得到所有这些。
普通的数组项看起来像;
[
"controller" => "RegisterController",
"method" => "GET",
"url" => "/register",
"action" => "index"
]
我正在使用我的 get_in_array 方法获取项目;
$routes = unserialize(apcu_fetch("routes"));
$route = get_in_array($this->url, $routes, "url");
助手
function get_in_array(string $needle,array $haystack,string $column){
$key = array_search($needle, array_column($haystack, $column));
// even if there are more than one same url, array search returns first one
if (!is_bool($key)){
return $haystack[$key];
}
}
但是 array_search()
方法只返回第一个匹配项。如果有两个具有相同 url 的数组(如 "/register"
),我无法同时获取它们。如何获得多个匹配结果?
最佳答案
在array_search
手册中提到:
To return the keys for all matching values, use
array_keys()
with the optionalsearch_value
parameter instead.
所以,而不是
$key = array_search($needle, array_column($haystack, $column));
使用
$keys = array_keys(array_column($haystack, $column), $needle); // notice order of arguments
关于php - 如何使用 array_search() 获取多个键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50822067/
我做密码翻译。我在翻译以点结尾的加密字符时遇到问题。我有以下字典: $dict = array( "I"=>"1", "L"=>"1.", "O"=>"0",
嘿,伙计们,女孩们,我被难住了。尝试让 array_search 使用此脚本。 **cannot find file to create array**"); //searc
Array ( [0] => 159042564114859_722842697734840 [1] => 180466813056_10152147419628057 [2]
我有一个数组 第一个: [0] => 0289 [1] => 0146 [2] => 5519 [3] => 5308 [4] => 5503 [5] => 5357 第二个(关联):
我有一个数组的 第一个: [0] => 0289 [1] => 0146 [2] => 5519 [3] => 5308 [4] => 5503 [5] => 5357 第二个(联想):
这里我有两张 table ,即房间和学生宿舍。房间看起来像这样 id rm_number capacity bed_no class hostel
我有一组要应用搜索功能的项目。我正在考虑对数组进行排序并暂时简单地应用二进制搜索,因为它不需要太复杂,但是如果我遇到问题,我会尝试其他方法。 我的问题是; array_search() 中使用的搜索算
这个问题在这里已经有了答案: PHP in_array() / array_search() odd behaviour (2 个答案) 关闭 6 年前。 我最近注意到在我的代码中使用 array_
我正在做一个日历,每天可能有超过 1 个事件与日期匹配。目前我很困惑如何发现比 array_search 的第一个匹配项更多的内容。这是我现在正在做的事情。 if (array_search($dat
这个问题在这里已经有了答案: PHP in_array() / array_search() odd behaviour (2 个答案) 关闭 6 年前。 我有这个数组: $ar = [ 'key1
PHP 的 array_search() 执行此操作: Searches the array for a given value and returns the corresponding key i
这里有两个表,即 tools 和 tool_use。 工具表看起来像这样 id name tools_names
我有一个简单的函数,它查看传入的 mySQL 数据类型,然后将其汇总到一个“类别”(在此代码中称为一个系列),以便我可以在类别级别应用默认值。不管怎样,这段代码在查找整数、字符和文本类别时工作正常,但
今天,我发现 php array_search 函数有一个很奇怪的问题。实际上我应用的条件是,如果索引是 0 或大于,它应该通过 IF 条件,否则不会,但它不会像那样工作。 我分析发现,如果输出是FA
在一维数组中使用 array_search 很简单 $array = array("apple", "banana", "cherry"); $searchValue = "cherry"; $key
$array_subjected_to_search =array( array( 'name' => 'flash', 'type' => 'hero'
我想从 $routes 数组中获取匹配的路由。如果有多个数组具有相同的“ur”值。我想得到所有这些。 普通的数组项看起来像; [ "controller" => "RegisterControl
我一直在为 array_search 苦苦挣扎,虽然我想我现在已经理解了,但我只想确保我完全理解我的代码执行方式背后的逻辑。 我正在尝试编写一个函数,如果元素不在数组中,则将其添加到数组中,如果是,则
以下代码将拆分一个句子并告诉我第一次出现单词 orange。你能告诉我是否可以使用 php 库函数找到第二次出现的单词 orange。我可以通过使用 for 循环 来做到这一点,只是想知道 array
我有这个代码 $restaurant = array('/restaurant_pos/', '/bar_nightclub_pos/') $current_page = $_SERVER['REQU
我是一名优秀的程序员,十分优秀!