1

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

2
  • 1
    Your question is about str_replace but didn't get it from your code, can you clarify ? Commented Nov 20, 2013 at 1:47
  • It is something i would like to add. Commented Nov 20, 2013 at 2:23

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks, where should I put this function in my code? Inside or outside public function?
You should of course create the function 'remove_dash' outside the function setFields, and as it is a member function you should call it like this: array_walk($myArray, array($this, 'remove_dash'));
Okay thanks. I placed the function below the function setFields and changed the array_walk. I am not very experienced with arrays, but when i put the code function remove_dash(&$item, $key) { if($item === '-') $item = ''; } $myArray = array("d" => "hello", "-", "b" => "test", "c" => "-"); array_walk($myArray, array($this, 'remove_dash')); in the code it results in a blank page. Am i doing something wrong? thanks!
well first of all, add the following at the beginning of your scripts: error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true); secondly, it's hard to tell without seeing the code, but have a look at this code that works: pastebin.com/raw.php?i=aGrqzPML
Thanks a lot Matthew, tried multiple things but the '_' is still there;) the code i have is too big, i guess, to post here. Thereby it is not a free code, so i cannot post it anywhere public i guess.. Thanks anyway for your help and i will keep trying!
|
0

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

1 Comment

Thanks, guess this is not the right place then because the '-' still shows up as sub-sub-category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.