颈椎骨质增生吃什么药效果好| 淋巴结炎吃什么药| 狂犬疫苗什么时候打有效| 月经期间吃什么水果| 海茸是什么东西| 内透声差是什么意思| 宿命是什么意思| 元辰是什么意思| 脚趾头麻木是什么原因引起的| 钧什么意思| 吃完饭就打嗝是什么原因| 丹参滴丸治什么病| 腿弯处的筋痛是什么原因| 石头记为什么叫红楼梦| 眼皮跳是什么原因引起的| 刘强东属什么生肖| 正方形体积公式是什么| 新生儿便秘怎么办什么方法最有效| angelababy是什么意思| 弱肉强食什么意思| 甘油三酯高吃什么食物好| 脚气用什么泡脚| rip什么意思| 独守空房是什么意思| 梦见发大水是什么预兆| 宫外孕是什么原因造成的| 手心发痒是什么原因| 梦见别人打我是什么意思| 吃什么减肥效果最好最快| aqua是什么牌子| 逍遥丸什么时候吃最好| 夜间睡觉口干口苦是什么原因| 处级上面是什么级别| 梦见自己被火烧是什么意思| 石钟乳是什么意思| 塞来昔布是什么药| 凉粉是用什么做的| 火星是什么意思| vg是什么意思| 稀松平常是什么意思| 数脉是什么意思| 及什么意思| 反流性食管炎吃什么食物好| 男士吃什么壮阳最厉害| 鼻血止不住是什么原因| 花团锦簇什么意思| 乳头瘙痒是什么原因| vte是什么| kgs是什么单位| 咳黄痰吃什么药| 右后背疼什么原因| 红蓝是什么意思| ups是什么快递公司| 克感敏又叫什么| 玉米吃多了有什么坏处| 病理是什么意思| 仙灵脾又叫什么| 看见喜鹊有什么预兆| 迎春花什么时候开| 属牛男和什么属相最配| 胆红素高是什么原因引起的| 吃生葵花籽有什么好处和坏处吗| 忽冷忽热是什么症状| 什么的搏斗| 霍启刚家族做什么生意| 付之东流是什么意思| 一清二白是什么意思| 乙肝有什么明显的症状| 小生化是检查什么项目| 双肺纹理增多模糊是什么意思| hpv是什么意思| 慢性肾炎是什么原因引起的| 思伤脾是什么意思| 什么芦荟可以直接擦脸| 掉头发吃什么药最有效| 肝郁血瘀吃什么中成药| 豆腐皮炒什么好吃| 口气重是什么原因| 放屁特别臭是什么原因| 腰扭伤吃什么药最有效| 山东简称为什么是鲁不是齐| 生肖牛和什么生肖最配| 碎花裙配什么鞋子| 什么叫发物| 中科院是干什么的| 经期喝咖啡有什么影响| 表情包什么意思| 吃地瓜叶有什么好处和坏处| 胃寒吃点什么药| 糖吃多了有什么危害| 相向是什么意思| 五花八门是指什么生肖| 男人湿气重吃什么药| 脑宁又叫什么名字| 宝宝大便发白是什么原因| 人生海海是什么意思| 顺理成章是什么意思| 木瓜有什么功效| dha是什么意思| 秦始皇原名叫什么| 荷叶茶有什么作用| 黄酒有什么功效与作用| 红细胞低吃什么补得快| luxury是什么牌子| 壁细胞主要分泌什么| 脑梗要注意什么| 64年属什么生肖| 冷藏和冷冻有什么区别| 气管炎吃什么食物好| 弱水三千只取一瓢什么意思| 金针菇为什么不能消化| 手蜕皮什么原因| 例假淋漓不尽是什么原因造成的| 学位证是什么| 白芍的功效与作用是什么| 92年什么命| tp是什么| 起鸡皮疙瘩是什么原因| 九什么一毛| 女性尿道炎挂什么科| dior什么意思| 唯小人与女子难养也什么意思| 梦到狗是什么征兆| 信必可是什么药| 菠萝蜜什么时候成熟| 哥弟属于什么档次| 右小腿抽筋是什么原因| 什么的春天| 灵芝搭配什么煲汤最好| 肚脐眼连接体内的什么器官| 胃气虚吃什么中成药| 月经量少吃什么| 缺席是什么意思| 高级别上皮内瘤变是什么意思| 手不自主颤抖是什么病| 尿潜血1十是什么原因| 拮抗药物是什么药| 1.18是什么星座| 水指什么生肖| 什么是黑户| 卵泡不破是什么原因造成的| 相刑什么意思| 下巴反复长痘痘是什么原因| oct是什么意思| 加拿大的国宝是什么动物| 青春不散场什么意思| 鹿的部首是什么| 口臭严重吃什么药好得快| 低密度脂蛋白是什么意思| 扦脚是什么意思| 趴着睡觉有什么坏处| 张姓为什么出不了皇帝| 脂肪肝轻度是什么意思| 骤雨落宿命敲什么意思| 造影检查对身体有什么伤害| 贫血会引起什么症状| 翻来覆去是什么意思| 梦见蛇蛋是什么意思啊| 蓝莓是什么季节的水果| 肾阳虚是什么意思| 昆虫记是什么类型的书| 什么发型适合自己| 梦见梳头发是什么意思| 胚胎停育是什么原因造成的| 黄什么鱼| 左眼皮跳是什么预兆| 止语是什么意思| 特勤是干什么的| 为什么手脚老是出汗| 中暑吃什么药好| 头皮痒是什么原因引起的| 复刻鞋是什么意思| 频发房性早搏是什么意思| 煲什么汤去湿气最好| 畅销是什么意思| hc是什么意思| 被蜜蜂蛰了涂什么药膏| 2.3什么星座| g6pd是什么| 孺子可教也什么意思| pdd是什么| 什么叫通勤| 给小孩办身份证需要什么| mb是什么意思| 早上手肿胀是什么原因| 尿酸低会引发什么症状| 梦到捡菌子是什么意思| 淋巴在什么位置| 猫五行属什么| 灌注治疗是什么意思| 梦见吃水饺是什么预兆| 什么是上火| 梦见别人生孩子预示什么| 屁很多是什么原因造成的| cip号是什么| 屎发黑是什么原因| 掉头发是什么原因女性| 校正是什么意思| 兔配什么生肖最好| 亡羊补牢说明什么道理| 杰瑞是什么品种的老鼠| 脖子痛挂什么科| 为什么总是莫名其妙的想哭| 什么晚霜比较好用| 吃什么补肾壮阳| 什么人不能喝桑黄| 尿潜血挂什么科| 子宫为什么长肌瘤| 验孕棒什么时候测最准| 41岁属什么| qjqj什么烟| 缺维生素a吃什么食物| 放血有什么好处| 发蜡和发泥有什么区别| 湿气重吃什么水果| 大使是什么行政级别| 突然好想你你会在哪里是什么歌| 渐入佳境什么意思| 梦见出国了是什么意思| 头发黄是什么原因| 输尿管不扩张什么意思| 什么是传染性软疣| 维生素b6是治什么的| 子宫什么样子图片| 尿道结石有什么症状| 樱花什么时候开| 雯是什么意思| 骨折有什么症状| 蛋蛋冰凉潮湿什么原因| 阑尾炎吃什么| 胃痛去药店买什么药| 生小孩有什么补贴政策| lr是什么意思| 为什么新生儿会有黄疸| 甘之如饴是什么意思| 盲肠憩室是什么意思| 运动减肥为什么体重不减反增| 足外翻挂什么科| 右眼皮跳是什么意思| a和ab型生的孩子是什么血型| 孩子流黄鼻涕吃什么药效果好| 何炅和谢娜是什么关系| 分期是什么意思| 尿不出尿是什么原因| 依巴斯汀片是什么药| 嘴苦口臭是什么原因造成的| 水淀粉是什么东西| 黄喉是什么动物身上的| 挫是什么意思| mchc是什么意思| 骨质疏松检查什么项目| 强烈的什么| 护士长是什么级别| 巡警是做什么的| 大便不调是什么意思| 脾阳虚吃什么食物好| 拉水便是什么原因| 经常尿路感染是什么原因| 梦到大牙掉了一颗是什么意思| 肺结核传染途径是什么| 病毒五项检查的是什么| 蛇形分班是什么意思| 什么食物补气血| 什么辉煌四字词语| 百度
rfc:array_find

管中闵硬起来反卡管 呛台“教育部”月底给答案

Introduction

百度   我被授予汉字书写艺术教育家,汉字艺术书写推广大使,全国教育系统优秀理论研究工作者,全国校外教育名师,北京市优秀教师,北京市骨干教师等称号。

This RFC proposes the addition of new array functions: array_find, array_find_key, array_any and array_all, which are helper functions for common patterns of checking an array for the existence of elements matching a specific condition.

There are currently several functions processing arrays using a callback. However still missing are functions to find a single element matching a condition and the closely related functions of checking whether or not there are elements matching a condition. Implementing these functions in userland is relatively simple, but these functions are often required, leading to the wheel being reinvented over and over. Furthermore these type of functions are implemented in other programming languages like Rust, JavaScript or C++, too. Therefore there is a reason to include these functions as standard with the next PHP version. In addition, the implementation of these functions is quite similar to array_filter and relatively trivial to implement, so the maintenance effort should be low.

Proposal

This RFC proposes to add four new function, array_find, array_find_key, array_any and array_all.

array_find

array_find returns the value of the first element for which the $callback returns true. If no matching element is found the function returns NULL.

function array_find(array $array, callable $callback): mixed {
    foreach ($array as $key => $value) {
        if ($callback($value, $key)) {
            return $value;
        }
    }
 
    return null;
}

Parameters

array $array

The array that should be searched.

callable $callback

The callback function to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns true, the value is returned from array_find and the callback will not be called for further elements.

Return Value

The function returns the value of the first element for which the $callback returns true. If no matching element is found the function returns NULL.

Examples

$array = [
    'a' => 'dog',
    'b' => 'cat',
    'c' => 'cow',
    'd' => 'duck',
    'e' => 'goose',
    'f' => 'elephant'
];
 
// Find the first animal with a name longer than 4 characters.
var_dump(array_find($array, function (string $value) {
    return strlen($value) > 4;
})); // string(5) "goose"
 
// Find the first animal whose name begins with f.
var_dump(array_find($array, function (string $value) {
    return str_starts_with($value, 'f');
})); // NULL
 
// Find the first animal where the array key is the first symbol of the animal.
var_dump(array_find($array, function (string $value, $key) {
   return $value[0] === $key;
})); // string(3) "cow"
 
// Find the first animal where the array key matching a RegEx.
var_dump(array_find($array, function ($value, $key) {
   return preg_match('/^([a-f])$/', $key);
})); // string(3) "dog"

array_find_key

array_find_key returns the key of the first element for which the $callback returns true. If no matching element is found the function returns NULL.

function array_find_key(array $array, callable $callback): mixed {
    foreach ($array as $key => $value) {
        if ($callback($value, $key)) {
            return $key;
        }
    }
 
    return null;
}

Parameters

array $array

The array that should be searched.

callable $callback

The callback function to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns true, the key is returned from array_find_key and the callback will not be called for further elements.

Return Value

The function returns the key of the first element for which the $callback returns true. If no matching element is found the function returns NULL.

Examples

$array = [
    'a' => 'dog',
    'b' => 'cat',
    'c' => 'cow',
    'd' => 'duck',
    'e' => 'goose',
    'f' => 'elephant'
];
 
// Find the first animal with a name longer than 4 characters.
var_dump(array_find_key($array, function (string $value) {
    return strlen($value) > 4;
})); // string(1) "e"
 
// Find the first animal whose name begins with f.
var_dump(array_find_key($array, function (string $value) {
    return str_starts_with($value, 'f');
})); // NULL
 
// Find the first animal where the array key is the first symbol of the animal.
var_dump(array_find_key($array, function (string $value, $key) {
   return $value[0] === $key;
})); // string(1) "c"
 
// Find the first animal where the array key matching a RegEx.
var_dump(array_find_key($array, function (string $value, $key) {
   return preg_match('/^([a-f])$/', $key);
})); // string(1) "a"

array_any

array_any returns true, if $callback returns true for any element. Otherwise the function returns false.

function array_any(array $array, callable $callback): bool {
    foreach ($array as $key => $value) {
        if ($callback($value, $key)) {
            return true;
        }
    }
 
    return false;
}

Parameters

array $array

The array that should be searched.

callable $callback

The callback function to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns true, true is returned from array_any and the callback will not be called for further elements.

Return Value

The function returns true, if there is at least one element for which $callback returns true. Otherwise the function returns false.

Examples

$array = [
    'a' => 'dog',
    'b' => 'cat',
    'c' => 'cow',
    'd' => 'duck',
    'e' => 'goose',
    'f' => 'elephant'
];
 
// Check, if any animal name is longer than 5 letters.
var_dump(array_any($array, function (string $value) {
    return strlen($value) > 5;
})); // bool(true)
 
// Check, if any animal name is shorter than 3 letters.
var_dump(array_any($array, function (string $value) {
    return strlen($value) < 3;
})); // bool(false)
 
// Check, if any array key is not a string.
var_dump(array_any($array, function (string $value, $key) {
   return !is_string($key);
})); // bool(false)

array_all

array_all returns true, if $callback returns true for all elements. Otherwise the function returns false.

function array_all(array $array, callable $callback): bool {
    foreach ($array as $key => $value) {
        if (!$callback($value, $key)) {
            return false;
        }
    }
 
    return true;
}

Parameters

array $array

The array that should be searched.

callable $callback

The callback function to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns false, false is returned from array_all and the callback will not be called for further elements.

Return Value

The function returns true, if $callback returns true for all elements. Otherwise the function returns false.

Examples

$array = [
    'a' => 'dog',
    'b' => 'cat',
    'c' => 'cow',
    'd' => 'duck',
    'e' => 'goose',
    'f' => 'elephant'
];
 
// Check, if all animal names are shorter than 12 letters.
var_dump(array_all($array, function (string $value) {
    return strlen($value) < 12;
})); // bool(true)
 
// Check, if all animal names are longer than 5 letters.
var_dump(array_all($array, function (string $value) {
    return strlen($value) > 5;
})); // bool(false)
 
// Check, if all array keys are strings.
var_dump(array_all($array, function (string $value, $key) {
   return is_string($key);
})); // bool(true)

Backward Incompatible Changes

Functions created by the user and named array_find, array_find_key, array_any or array_all lead to a PHP error with the new version. A quick GitHub search shows, that there a 656 results defining the symbol array_find, 28 results defining the symbol array_find_key, 127 results defining the symbol array_any and 284 results defining the symbol array_all for the language PHP.

Looking at the search results I estimate about 30% of these results are functions that are not located in a namespace, are not part of a class and are not false-positvies (e.g. the symbol is db_array_all instead of array_all).

Proposed PHP Version(s)

PHP 8.4

RFC Impact

To SAPIs

None.

To Existing Extensions

None.

To Opcache

None.

New Constants

None.

php.ini Defaults

None.

Open Issues

This function behaves differently to array_filter, because it always passes the value and the key to the callback. array_filter has a parameter for this to set which parameters are to be passed to the callback. array_map does not support the pass of the key at all, but support using more than one array, to pass a various number of parameters to the callback. With closures, omitting a parameter is now possible without any problems, therefore, there is no reason to determine the choice of parameters for the callback.

Unaffected PHP Functionality

This RFC only adds two new functions to PHP and only affects previously defined functions which are named as the proposed functions.

Proposed Voting Choices

Each vote requires a 2/3 majority.

Voting started on 2025-08-04 and will end on 2025-08-04 15:00 UTC.

array_find()/array_find_key()

Add array_find() and array_find_key()?
Real name Yes No
alcaeus (alcaeus)  
ashnazg (ashnazg)  
beberlei (beberlei)  
cpriest (cpriest)  
crell (crell)  
derick (derick)  
galvao (galvao)  
jimw (jimw)  
josh (josh)  
kguest (kguest)  
levim (levim)  
mbeccati (mbeccati)  
mfonda (mfonda)  
nicolasgrekas (nicolasgrekas)  
nielsdos (nielsdos)  
petk (petk)  
ramsey (ramsey)  
seld (seld)  
sergey (sergey)  
timwolla (timwolla)  
weierophinney (weierophinney)  
Final result: 21 0
This poll has been closed.

array_any()/array_all()

Add array_any() and array_all()?
Real name Yes No
alcaeus (alcaeus)  
ashnazg (ashnazg)  
cpriest (cpriest)  
crell (crell)  
derick (derick)  
galvao (galvao)  
jimw (jimw)  
josh (josh)  
kguest (kguest)  
levim (levim)  
mbeccati (mbeccati)  
mfonda (mfonda)  
nicolasgrekas (nicolasgrekas)  
nielsdos (nielsdos)  
petk (petk)  
ramsey (ramsey)  
seld (seld)  
sergey (sergey)  
timwolla (timwolla)  
weierophinney (weierophinney)  
Final result: 20 0
This poll has been closed.

Implementation

References

rfc/array_find.txt · Last modified: by 127.0.0.1

?
胃粘膜脱落什么症状严重吗 洄游是什么意思 胸口闷痛挂什么科 12什么意思 滑膜炎吃什么药
筷子什么材质最好 医院康复科是干什么的 吃中药忌口都忌什么 痧是什么 缺维生素d有什么症状
哺乳期什么东西不能吃 活在当下什么意思 覆盆子是什么东西 巴马汤泡脚有什么功效 梦见玉碎了是什么意思
女频是什么 刘五行属什么 大学是什么学历 喝脱脂牛奶有什么好处 护照类型p是什么意思
艾滋病会有什么症状hcv9jop7ns0r.cn 猪狗不如是什么意思hcv9jop3ns8r.cn 心内科全称叫什么wzqsfys.com 眼角发白是什么原因hcv7jop6ns7r.cn 吃花生米有什么好处hcv9jop8ns2r.cn
头胀痛吃什么药hcv9jop8ns1r.cn 眼前有亮光闪是什么问题hcv9jop6ns3r.cn 7月去青海带什么衣服hcv9jop2ns4r.cn asus是什么牌子hcv9jop1ns2r.cn 指是什么意思hcv8jop4ns9r.cn
azul是什么颜色hanqikai.com 蛤蚧是什么hcv8jop8ns6r.cn 胃反酸水是什么原因hcv8jop1ns6r.cn 吃什么可以淡化黄褐斑hcv9jop3ns8r.cn 嬴政姓什么hcv9jop3ns1r.cn
青梅是什么水果tiangongnft.com 什么叫做亚健康1949doufunao.com 燕窝是什么520myf.com 舌头两边疼是什么原因hcv9jop0ns2r.cn 飞机是什么意思hcv8jop1ns8r.cn
百度