PHP:使用array_filter [英] PHP: Working with array_filter

查看:91
本文介绍了PHP:使用array_filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上与先前的问题有关,我问有人在哪里可以提供一些我可以使用的代码来解决我提出的问题。

This actually relates to a previous question I asked where someone kindly offered some code I could work with as a solution to a problem I posed.

现在,我明白了

作为参考,这是我提出的上一个问题;直到我到达使用array_filter检索重复行的位置为止。 PHP:以OOP方式搜索CSV文件

For reference, this is the previous question I posed; PHP: Searching through a CSV file the OOP way

这是我很难完全了解发生了什么的地方;

This is where I'm having difficulty fully understanding what's going on;

public static function getRowsWithDuplicates($columnIndex) {
    $values = array();
    for ($i = 0; $i < $this->_dataSet->getRowCount(); ++$i) {
        $values[$this->_dataSet->->getValueAt($i, $columnIndex)][] = $i;
    }

    return array_filter($values, function($row) { return count($row) > 1; });
}

首先;查看该代码,在我看来$ values是一个关联数组。如果是这样,那么array_filter如何表现为返回具有重复值的行?

Firstly; looking at that code, it looks to me like $values is an associative array. If so, then how does array_filter behave to return rows with duplicate values?

我想我不完全理解array_filter发生的过程来理解这一部分。码;我知道array_filter会将数组中的每个值传递到其参数表中提供的函数中,然后从该函数中返回一个值,但是在这个特定示例中,我认为我不理解array_filter中到底发生了什么。

I don't think I fully understand the process of what's happening with array_filter to understand this piece of code; I know that array_filter passes each value from the array into the function provided in its paremeters, and then returns a value from that function, but in this particular example I don't think I understand what's happening exactly inside array_filter.

如果有人可以在逐步理解该过程的水平上向我解释它,我将不胜感激,这样我就可以更好地理解上述代码中发生的事情,因此我

I'd appreciate if someone could explain it to me at a level where I can understand the process step by step, so I can gain a better understanding of what's happening in the above code so I can go beyond just replicating the results.

如果$ values是一个关联数组,那么代码如何利用关联数组的属性返回重复值呢?

If $values is an associative array, than how is the code taking advantage of the properties of associative array to return duplicate rows?

我想这并不是我很难理解的函数本身,而是在特定情况下如何使用它。

I guess it's not really the function itself I'm having difficulty understanding, but rather how its been used in this particular case.

return count($row) > 1;

我不了解比较运算符在返回值时的表现;是不是说只有多于1行才返回TRUE,然后array_filter评估该TRUE语句并返回与其关联的值?

I don't understand how the comparative operator behaves when returning a value; is it saying only return TRUE when there are more than 1 rows, and then array_filter is evaluating that TRUE statement and returning the value associated with it?

传递给$的内容在函数($ row)中排成一行?

What's being passed into $row in function($row)?

您可以看到,我的问题比答案要多得多,我宁愿向我解释而不是给我解释

As you can see, I have a lot more questions than I do answers, and I would rather it were explained to me than I spend too long speculating and come to incorrect conclusions.

$ row只是array_filter用于传递其迭代的数组值的参数吗?

Is $row just the parameter that array_filter uses to pass in the array value its iterating through?

编辑:这是我修改过的问题,它更具体,指向我要寻找答案的地方;

我了解array_filter在做什么,但我不知道它是如何工作的。代码的原始发布者乔恩(Jon)写道;
此代码将返回一个数组,其中键是CSV数据中的值,并且值是每个值出现的行的索引都从零开始的数组。

I understand what array_filter is doing, but I do not understand how it is doing it. The original poster of the code, Jon, wrote; "This code will return an array where the keys are values in your CSV data and the values are arrays with the zero-based indexes of the rows where each value appears."

这是我不明白的;到什么时候$ values的值是数组,每个值出现的行的索引都从零开始。代码如何查找每个值出现的每一行的索引,并将其放入存储在$ values中的数组中?

This is what I do not understand; How is it getting to the point that $values has values that are arrays with zero-based indexes of the rows where each value appears. How is the code finding the indexes of each row where every value appears, and putting that into arrays stored within $values?

所以我的问题与这段代码有关:

So my question relates to this part of the code:

for ($i = 0; $i < $this->_dataSet->getRowCount(); ++$i) {
    $values[$this->_dataSet->->getValueAt($i, $columnIndex)][] = $i;

代码如何找到所有匹配值的索引,将它们放在数组中,数组的关键是索引所涉及的值?

How does the code find the all the indexes for matching values, put those in an array, where the key of that array is the value that the indexes relate to?

这就像整个重复值的搜索一样,我看不到。

It's like a whole search for duplicate values is going on that I can't see.

推荐答案

array_filter 中的回调应返回 true false 。如果返回 true ,则表示应保留当前值。如果返回 false ,则应舍弃当前值。

The callback in array_filter should return either true or false. If it returns true, then that means the current value should be kept. If it returns false, then the current value should be discarded.

数组的每个值都传递给回调函数作为其第一个参数(在这种情况下为 $ row ),然后由回调函数确定是否保留该值。

Each value of the array is passed to the callback function as its first argument ($row in this case), then it's up to the callback to determine whether or not to keep the value.

这篇关于PHP:使用array_filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆