简体   繁体   English

展平这个数组堆栈 PHP

[英]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我正在输出 sha1 转换,但我希望将其分成 10 个的 4 个部分

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. $newar已经是你想要的了,假设你想要它作为一个字符串。

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.不确定这是否是正确的做事方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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