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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题