0

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...

2
  • Did you print all the values of $key2 and $value2 to see what the values are? Perhaps using error_reporting(E_ALL); could help while debugging Oct 10, 2021 at 9:26
  • Yeah, I did print those, they print fine... But when pushing them into the array things start getting weird Oct 10, 2021 at 11:07

1 Answer 1

0

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.