Reputation: 1309
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?
Upvotes: 0
Views: 583
Reputation: 1309
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!!
Upvotes: 0
Reputation: 595
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.
Upvotes: 1