RobbertT
RobbertT

Reputation: 253

str_replace in an Array?

I was wondering if it is possible to do an str_replace within an array. I have a script that gets values from a CSV file and put it into a mysql database. However cells within the csv file might contain '-', in order to indicate that there is no value. The current script will, however, will import the value '-' into the mysql database. What i would like is that it will ignore the value '-' or replace it with '' (an empty cell value) so that no value is imported into the mysql databse.

This is what the current array looks like (part of the script). I would like to apply the filter to: sub_sub_category

public function setFields($dir = 'import')

  {

    $timeNow = date('Y-m-d H:i:s');

    if ($this->v14)

      $this->_path = realpath(_PS_ADMIN_DIR_.'/'.$dir).DIRECTORY_SEPARATOR;

    else

      $this->_path = realpath(PS_ADMIN_DIR.'/'.$dir).DIRECTORY_SEPARATOR;

    $this->psFields1a = array(
'sub_sub_category' => $this->l('Sub-sub-category'));
}

I hope it is clear what i mean and that it is a doable thing to do. Thanks, Robbert

Upvotes: 1

Views: 221

Answers (2)

Matthew
Matthew

Reputation: 11260

the following code demonstrates how you can remove all the "-" hyphens from an array using array_walk.

    function remove_dash(&$item, $key) {
        if($item === '-') $item = '';
    }

    $myArray = array("d" => "hello", "-", "b" => "test", "c" => "-");

    array_walk($myArray, 'remove_dash');

(hope I udnerstood the question)

Upvotes: 2

Fluffeh
Fluffeh

Reputation: 33502

I am hoping that this is the right line you are looking at, but why not just do it as you are setting it?

$this->psFields1a = array(
sub_sub_category => str_replace('-','',$this->l('Sub-sub-category'))
);

Upvotes: 0

Related Questions