douci2516 2012-02-20 13:56
浏览 49
已采纳

PHP:许多阵列合而为一

I am doing a small calculator, that has randomly selected sequence of 3 chars (-, / and \).

How can I combine my three arrays into one? I'm using array_merge, but it creates an array that nests 3 arrays in it. How can I make this array one-dimensional?

My code (feedback welcome):

$_SESSION["sekvenssi"] = 15; //sekvenssi means "sequence" in Finnish.

$maara1 = rand(1, $sekvenssi/2);  //Maara means "amount"
$maara2 = rand(1, $sekvenssi/2);

$kenot = array_fill(0, $maara1, "/");
$vastakenot = array_fill(count($kenot), $maara2, "\\");
$tayte = array_fill(
    count($kenot)+count($vastakenot),
    $_SESSION["sekvenssi"]-$maara1-$maara2,
    "-"
);

$kaava = array_merge(array($tayte), array($kenot), array($vastakenot));
shuffle($kaava);

print "<p style='font-family: Courier;font-size: 18px;'>";
    foreach ($kaava as $komponentit) {
    print $komponentit;
}
print "</p>";

So this outputs "ArrayArrayArray", but I want it to produce one array with only one level (now it has 2 levels and thus cannot be printed).

  • 写回答

2条回答 默认 最新

  • dqwh2717 2012-02-20 13:59
    关注

    You have already created the array's when you use array_fill, so when you merge them, you are then creating another set of arrays with each array.

    Try changing this:

    $kaava = array_merge(array($tayte), array($kenot), array($vastakenot));
    

    to

    $kaava = array_merge($tayte, $kenot, $vastakenot);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?