0

I am creating password generator, I am dealing at the moment with array of special characters to transfer them into string. I have special characters saved in cvs file, using as array to slice based on how many special characters should be in password, then I want to make them string and concatenate with numbers and letters.

    $list = './SpecialChar.csv';
    $e = array_map('str_getcsv', file($list));

    //$nRange telling how many characters should be slice
    $nRange = $length-($numb*2)-$specialChar;

    shuffle($e);
    $s = array_slice($e,0,$nRange);
    $sString = implode(" ",$s); //does not work
    $sString = htmlentities(implode(" ",$s)); //does not work
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
NKU
  • 13
  • 4

1 Answers1

0

implode only accepts string so if you try to convert array to string this will return you "string(5) "Array"" no matter what it's in the array

var_dump((string)["T","E","S","T"]);

So when you try to implode multidimensional array you will get something like this

    $test = [["-"], ["*"]];
var_dump(implode(" ", $test)); //THIS WILL GIVE YOU Notice: Array to string conversion when using implode , but it will return string(11) "Array Array"

If dimensional are only two you can use array_map

$test = [["-"], ["*"]];
var_dump(implode(" ", array_map(function ($row) {
                        return is_array($row)?implode($row):$row;
                    }, $test))); //returns string(3) "- *"
angel.bonev
  • 2,154
  • 3
  • 20
  • 30
  • thank @angel.bonev, I am still trying to figure then how to concatenate strings of $letters and $numbers. ``` $n = range(0,9); shuffle($n); $numbers = array_slice($n,0,$nRange); $numbersString = implode(" ",$numbers);```, because $test is still just array, looks like I have safe all special characters in array, but it does not work, if I am creating regular array – NKU Aug 05 '19 at 13:46
  • @NKU Implode expects one dimensional array you can't concatenate multydimensional arrays only with implode, it converts every element to string, so you can foreach all you data and merge it into one dimensional array or use something like array_map – angel.bonev Aug 05 '19 at 15:19