PHP: Working with array_filter

2019-09-12 15:10发布

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.

Now, I understand most of code that was posted, until I get to where array_filter has been used to retrieve duplicate rows.

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; });
}

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?

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.

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;

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?

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.

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

Edit: This is my revised question, which is more specific and to the point of what I am looking for an answer on;

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."

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.

1条回答
唯我独甜
2楼-- · 2019-09-12 15:40

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.

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.

查看更多
登录 后发表回答