Question

I don't know what I have done wrong with the code below but it is not working.

if($_data_count = count($_csv_raw_array) > 0){
            foreach($_csv_raw_array as $_csv_row){
                array_push($this->_data, array_map(call_user_func(array($this, 'replace')), $_csv_row));
            }
            return $this->result(true, 'CSV parsing done.', '', $this->_data);
        }

// And the replace function is:

    private function replace($_value){
    return preg_match('/(\r\n|\n|\r)/', $_value) ? '' : $_value;
}

But I don't know why it is not working and throws an exception:

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Import::replace()

Filename: libraries/Import.php

Line Number: 116

Basically what I wanted to to here is import a CSV file which I already imported, now I want to replace any new line characters that is present within the array to be replaced with empty (nothing). But every time I execute this code it throws an exception. Can you people out there suggest. Please.

Thanx

Was it helpful?

Solution

As pointed out in the comments, judging by your code you will want to drop that call_user_func, since that is causing the function to be called immediately and that does not seem to be what you want, which I presume is to filter out all the elements within $_csv_row in each iteration:

if($_data_count = count($_csv_raw_array) > 0){
    foreach($_csv_raw_array as $_csv_row){
        array_push($this->_data, array_map(array($this, 'replace'), $_csv_row));
    }
    return $this->result(true, 'CSV parsing done.', '', $this->_data);
}

We can only guess what is your intention though, you might want to provide a sample csv and the desired output so we can understand more precisely what you are looking for if this doesn't solve the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top