简体   繁体   中英

flattening this array stack PHP

I'm trying to find a way to flatten this array

I'm outputting a sha1 conversion but I wish to break it into 4 parts of 10

then change its order before saving it.

here is my code so far.

 <?php $hash = bin2hex(random_bytes(5)); $randomhash = sha1($hash); $parts = str_split($randomhash, 10); $newar = $parts[3].$parts.[1].$parts.[2].$parts[0]; $hashflat = implode(" ",$newar); echo $hashflat;

However, this does not work as desired. Is there a simple way to implode

$parts[3] $parts [1] $parts [2] $parts[0]

in this order to a flat output?

$newar will already be as you want it, assuming you want it as a string.

However, you want spaces so it'll be:

$hash = bin2hex(random_bytes(5));
$randomhash = sha1($hash);
$parts = str_split($randomhash, 10);
$newar = $parts[3]." ".$parts[1]." ".$parts[2]." ".$parts[0];

this will return it as a string with spaces rather than an array. if you want it as a reordered array it's

$hash = bin2hex(random_bytes(5));
$randomhash = sha1($hash);
$parts = str_split($randomhash, 10);
$newar = array($parts[3],$parts[1],$parts[2],$parts[0]);
$newar = array($parts[3],$parts[1],$parts[2],$parts[0]);

setting a new array seemed to work in this case for me.

not sure if this is the correct way of doing things though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM