Issue
I'm trying to build an array with a few variations of some data, when using preg_replace and echoing out values, it works like normal, but if I try to store those values in an array, the function simply returns nothing, and gives no error.
Here's a piece of the code I'm using, it's inside a class.
$actions = array();
foreach($controllers as $ck => $cv) {
$cvar = 'br'.$ck.'actions';
foreach(self::$$cvar as $key => $value) {
if ($key != $value) {
$actions[$value] = $key;
if( preg_match('/[A-Z]/',$key)!==0 || preg_match('/[A-Z]/',$value)!==0 )
{
$key2=strtolower(preg_replace('/(?<=\\w)([A-Z])/','-\\1',$key));
$value2=strtolower(preg_replace('/(?<=\\w)([A-Z])/','-\\1',$value));
$actions[$value2] = $key2; // Everything works except for this line, if I comment it, it works, if I don't, it simply doesn't return even an error.
}
}
}
}
return $actions;
What is causing this weird behaviour? It should be able to add to the array like normal, but it doesn't...
Solution
So, I found out what was the problem:
In the arrays of values being used to create the $actions array (The $br(controller)actions array) there were some values that accidentally had some diacritics on them (Like the word "DemonstraĆ§Ć£o"), which when going through the preg_replace function, became some of those black question mark boxes (�� Replacement Characters), which then were fed as array keys by the function and somehow made the function return empty instead of the array. Printing the values showed that the function still ran through all the loop iterations, but the array simply didn't return properly with those values on it.
I fixed it by removing the diacritics, then the method ran smooth as it should.
Answered By - Miguel Vieira Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.