doumindang2416 2012-11-26 23:58
浏览 66

too long

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条回答 默认 最新

  • dtw52353 2012-11-27 00:01
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算