0

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

bn00d
  • 1,126
  • 3
  • 15
  • 21
  • You should remove `call_user_func` entirely because that is causing the function to be called immediately; instead use: `array_map(array($this, "replace"), $_csv_row)` – Mahn Sep 10 '12 at 10:55
  • @Mahn, err no, replace is being called without an argument. – dbf Sep 10 '12 at 11:11
  • @dbf because the argument would normally be passed by array_map if it wasn't because the function is being called immediately due to call_user_func, which by the looks of OPs code, is not what he intended to do. You can do `array_map("functionThatExpectsArgument", $array)`, but you can't do: `array_map(call_user_func("functionThatExpectsArgument"), $array)` – Mahn Sep 10 '12 at 11:14
  • first argument of [array_map](http://php.net/manual/en/function.array-map.php) is a callable, you definitely will get an error because in your example it will have an array. – dbf Sep 10 '12 at 11:25
  • @dbf nope, that is the standard way of pointing to callable object properties without using closures, passing array($this, "replace") will just point to $this->replace() and validate as a callable argument. See: http://www.php.net/manual/en/language.types.callable.php – Mahn Sep 10 '12 at 11:54
  • @Mahn, just to be clear, I'm referring to your `array_map(array($this, "replace"), $_csv_row)`. Just trying to point out that `array_map` will **not** work this way, only `call_user_func` in such an approach, sorry ;) (or is it a revision change in 5.4?) – dbf Sep 10 '12 at 12:22
  • @dbf Yes it will. Again, `array($this, "replace")` will validate as a callable argument, quoting the php doc: `A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.` – Mahn Sep 10 '12 at 12:27
  • And call_user_func is definitively not what you want to use with array_map, since (again) this calls the function immediately instead of passing it as an argument. – Mahn Sep 10 '12 at 12:28
  • Hmm, local version of PHP 5.3.11 does not accept `array_map(array($this,"mthd"),array('val1','val2'))` but on a diff. server with 5.3.14 and .16 it does, funny .. time for an upgrade then ;) – dbf Sep 10 '12 at 12:40

1 Answers1

0

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.

Mahn
  • 16,261
  • 16
  • 62
  • 78