合并子阵列并从结果数组中获取随机值

时间:2016-02-04 16:32:34

标签: php arrays

我正在尝试为值数组或单个字符串创建字符串排列,这就是我正在做的事情:

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);

代码很好但我有一些问题,我需要一些帮助。如果您在控制台脚本中运行上面的代码,您应该看到如下所示的输出:

# 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输出很好,但test1不是。如果您将$names的内容转储到internalProcessing()(就在返回之前),您将获得以下内容:

// 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',
  ...
)

我需要将输出转换为:

$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',
    ...
  )

最后,如果输入是一个值数组,那么我应该返回0和count之间的任何随机值($ result_array)。我试过array_merge()但是因为我的结果错了所以对我不起作用。可以给我一些推动吗?

1 个答案:

答案 0 :(得分:1)

你确实有数组数组,因为 internalProcessing 返回一个数组,你把这个结果作为一个数组元素放在一个包装数组中:

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

相反,您应该按如下方式存储 internalProcessing 的结果(在两次出现时):

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

这就是你可以在fiddle中看到的诀窍。