0

I am trying to create a string permutation for an array of values or for a single string and this is how I am doing:

function aliasName($string)
{
    $names = [];

    if (is_array($string)) {
        foreach ($string as $str) {
            $names[] = internalProcessing($str);
        }
    } else {
        $names[] = internalProcessing($string);
    }

    return $names[array_rand($names)];
}

function internalProcessing($str)
{
    $names = [];

    $input = strtolower($str);
    $length = strlen($input);
    $counter = pow(2, $length);

    for ($i = 0; $i < $counter; ++$i) {
        $binaryStr = str_pad(decbin($i), $length, '0', STR_PAD_LEFT);

        $variant = '';
        for ($j = 0; $j < $length; ++$j) {
            $variant .= ($binaryStr[$j] == '1') ? strtoupper($input[$j]) : $input[$j];
        }

        $names[] = $variant;
    }

    return $names;
}

$test1 = ['Mami', 'Abonis', 'Acierto', 'Alison', 'Bato', 'Chiste', 'Amada', 'Calor', 'Dato', 'Fiera', 'Exito'];
$test2 = 'mamI';

$test1Result = aliasName($test1);
$test2Result = aliasName($test2);

echo "\n test1 -------------------------------- \n";
var_export($test1Result);
echo "\n test2 -------------------------------- \n";
var_export($test2Result);

The code works good but I have a few issues and I need some help. If you run the code above in a console script you should see an output like the one below:

# php test.php

 test1 --------------------------------
array (
  0 => 'abonis',
  1 => 'aboniS',
  2 => 'abonIs',
  3 => 'abonIS',
  4 => 'aboNis',
  5 => 'aboNiS',
  6 => 'aboNIs',
  7 => 'aboNIS',
  8 => 'abOnis',
  9 => 'abOniS',
  10 => 'abOnIs',
  ...
)

 test2 --------------------------------
array (
  0 => 'mami',
  1 => 'mamI',
  2 => 'maMi',
  3 => 'maMI',
  4 => 'mAmi',
  5 => 'mAmI',
  6 => 'mAMi',
  7 => 'mAMI',
  8 => 'Mami',
  9 => 'MamI',
  10 => 'MaMi',
  ...
)

test2 output is just fine, but test1 is not. If you dump the content of $names at internalProcessing() (just before the return) you'll get something like:

// var_export($names);  
// return $names;

# php test.php
array (
  0 => 'mami',
  1 => 'mamI',
  ...
)array (
  0 => 'abonis',
  1 => 'aboniS',
  ...
)array (
  0 => 'acierto',
  1 => 'aciertO',
  ...
)array (
  0 => 'alison',
  1 => 'alisoN',
  ...
)array (
  0 => 'bato',
  1 => 'batO',
  ...
)array (
  0 => 'chiste',
  1 => 'chistE',
  ...
)array (
  0 => 'amada',
  1 => 'amadA',
  ...
)array (
  0 => 'calor',
  1 => 'caloR',
  ...
)array (
  0 => 'dato',
  1 => 'datO',
  ...
)array (
  0 => 'fiera',
  1 => 'fierA',
  ...
)array (
  0 => 'exito',
  1 => 'exitO',
  ...
)array (
  0 => 'mami',
  1 => 'mamI',
  ...
)

I need to transform that output to something like:

$result_array = array (
    0 => 'mami',
    1 => 'mamI',
    ...,
    0 => 'abonis',
    1 => 'aboniS',
    ...,
    0 => 'acierto',
    1 => 'aciertO',
    ...,
    0 => 'alison',
    1 => 'alisoN',
    ...,
    0 => 'bato',
    1 => 'batO',
    ...,
    0 => 'chiste',
    1 => 'chistE',
    ...,
    0 => 'amada',
    1 => 'amadA',
    ...,
    0 => 'calor',
    1 => 'caloR',
    ...,
    0 => 'dato',
    1 => 'datO',
    ...,
    0 => 'fiera',
    1 => 'fierA',
    ...,
    0 => 'exito',
    1 => 'exitO',
    ...,
    0 => 'mami',
    1 => 'mamI',
    ...
  )

And finally if the input is an array of values then I should return any random values between 0 and count($result_array). I have tried array_merge() but didn't works for me since I got wrong result. Can any give me some push here?

1
  • What is wrong with output of test1, that is not wrong in test2? Commented Feb 4, 2016 at 16:38

1 Answer 1

1

You have indeed arrays of arrays, as internalProcessing returns an array, and you put that result as an array element in a wrapping array with this:

 $names[] = internalProcessing(...);

Instead you should store the result of internalProcessing as follows (in both occurrences):

 $names = array_merge($names, internalProcessing(...));

That does the trick as you can see in this fiddle.

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

3 Comments

Didn't work for me, I am getting the same output array of arrays, did you tried?
nvm, since the first result is a multi-dimensional array then I've changed from array_merge to array_merge_recursive and that does the job. Thank for the heads up!
With the example you had given there is no need for array_merge_recursive (I added a fiddle), but I am glad it works out for you.

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.