简体   繁体   English

PHP发生了什么-我听不懂?

[英]What is going here PHP - I don't understand?

I have an assocaitive array with all different kind of values, but what is below will find all the array keys that end in Study and check the boolean value of it, but what I don't understand is how it works anyway care to explain a step by step explanation of it? 我有一个具有所有不同种类值的可组合数组,但是下面的内容将找到所有以Study结尾的数组键并检查其布尔值,但是我不明白的是它是如何工作的,无论如何都在乎解释一个一步一步的解释吗?

$study = array_filter(array_intersect_key($user, array_flip(preg_grep('/Study$/', array_keys($user)))));

if ($study) {
     ......
}

How does php sort the keys that end in Study ? php如何对以Study结尾的键进行排序?

And are there any better ways to do this? 还有没有更好的方法可以做到这一点?

Expressions with nested function calls are evaluated from the inner to the outermost parens. 带有嵌套函数调用的表达式是从内部到最外面的括号求值的。 The order of execution is as follows: 执行顺序如下:

  1. array_keys($user) - turns the array into an key list array_keys($user) -将数组变成键列表
  2. preg_grep('/Study$/', - extracts specific keys preg_grep('/Study$/', -提取特定的键
  3. array_flip( - turns the key list back into an associative key array array_flip( -将键列表变回关联的键数组
  4. array_intersect_key($user, - uses that filtered associative key list to reduce the original array to the wanted subset array_intersect_key($user, -使用过滤后的关联键列表将原始数组缩小为所需的子集
  5. array_filter( - removes non-true entries (NULL and false) array_filter( -删除非true条目(NULL和false)
  6. $study = - assigns result $study = -分配结果

array_keys($user) returns the keys as values, and preg_grep('/Study$/' filters that list to return only those values that end (the $ is the end anchor) with the letters "Study", giving us a set of filtered key values. array_keys($ user)返回键作为值,而preg_grep('/ Study $ /'过滤器列出仅返回那些以字母“ Study”结尾($是结束锚点)的值,从而为我们提供了一组过滤后的键值。

array_flip then inverts the filtered array so that the values become keys in a new array that contains only entries with the filtered keyset. 然后,array_flip反转过滤后的数组,以便这些值成为新数组中的键,该数组仅包含具有过滤后的键集的条目。

That subset of keys is then matched with the original array keys by the array_intersect_key() function, so that only entries from the original array that have keys matching the keys in the filtered array are returned. 然后,通过array_intersect_key()函数将该键的子集与原始数组键进行匹配,以便仅返回原始数组中具有与过滤后的数组中的键匹配的键的项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM