0

I have made a class that should replace data at the hand of an array.

I insert

$data = array(
                'bike',
                'car',
                'pc'
            );

And it should translate the array to :

$data = array(
                '[@bike]',
                '[@car]',
                '[@pc]'
            );

I have written the following piece of code.

array_walk($PlaceHolders , function(&$value, $key) { $value = $this->Seperators['plcstart'].$value.$this->Seperators['plcend']; });

Which I am using in a class context.

The two sperators have the following value:

$this->Seperators['plcstart'] = '[@';
$this->Seperators['plcend']   = ']';

The problem is. At localhost it works superb!! Now when I upload the system to a unix environment it stops working! I have no clue why this is the result.

Would anyone here know what I am doing wrong?

4
  • Start by considering the different versions of PHP that are running on your development/localhost environment and on your production environment.... and consider why they are different when they shouldn't be Commented Nov 27, 2014 at 18:47
  • array_walk won't invoke your object, the function you're defining as the callback won't be a method of your object either. it's just a plain regular function, and $this won't be available. Commented Nov 27, 2014 at 18:48
  • Where and how are the values of $this->Seperators defined? Is all this defined within a class method? Commented Nov 27, 2014 at 18:59
  • The $this->Seperators are defined within the class. I think it might be a good idea to set the Seperators in a loose variable. $Start = $this->Seperators['plcstart']; etc.. Commented Nov 27, 2014 at 19:27

2 Answers 2

1

I would recommend you to use array_map() when you want to modify array elements using a callback.

You could end with something like this

class TransformerClass {
    private $Seperators = array(
        'plcstart' => '[@',
        'plcend' => ']'
    );

    public function transform(array $data) {
        return array_map(
            function($text) {
                return $this->Seperators['plcstart'].$text.$this->Seperators['plcend'];
            },
            $data
        );
    }
}

Example:

$transformer = new TransformerClass();

$items = array(
    'bike',
    'car',
    'pc'
);

$result = $transformer->transform($items);

And $result will contain your desired resulting data.

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

Comments

0

The most bizare thing is... This morning I opened the same page again. And it worked like the offline environment :-S....

This is quite frustrating though!!

However I would like to make a few small notes:

  ///######## LOADING ALL PLACEHOLDERS
  $PlaceHolders   = array_keys($this->TplObjects);

  ///######## SEPERATOR START
  $SeperatorStart = $this->Seperators['plcstart'];
  $SeperatorEnd   = $this->Seperators['plcend'];

  ///######## ADDING THE START AND END TAGS TO EACH ARRAY VALUE
  array_walk($PlaceHolders , function(&$value, $key) { $value =
  $SeperatorStart.$value.$SeperatorEnd; });

This is what I thought to be a solution but it is NOT!! Why?? Because the way it was worked:

    ///######## LOADING ALL PLACEHOLDERS
    $PlaceHolders = array_keys($this->TplObjects);
    ///######## ADDING THE START AND END TAGS TO EACH ARRAY VALUE
    array_walk($PlaceHolders , function(&$value, $key) { $value = $this->Seperators['plcstart'].$value.$this->Seperators['plcend']; });

Because it got it's data directly out of the class. By using function I placed the variables into it's own individual scope, hence the variable $SeperatorStart and $SeperatorEnd do not exist in this scope.

I could ofcourse import these two into that function. But I do not know how to do this with array_walk. I have not used this function this often hence I know only a few basic manners to use this function.

The second option as opted for above by @Falc works great! And is the method I was looking for. Hence thanks a million!!

Comments

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.