skip to Main Content

Here is my problem.
I have created two bunny’s with character and want to display them, als with PHP.

For the two bunny’s I created a multidimensional array:

$bunnys = array(
              array( //$bunny
                  array(' ', ' ', '(', ')', ' ', '(', ')', ' ', ' '), //$row en in de array is $piece
                  array(' ', ' ', '(', 'o', '.', 'o', ')', ' ', ' '),
                  array(''', '(', '"', ')', ' ', '(', '"', ')', '''),
              ),
              array(
                  array(' ', ' ', '(', ')', ' ', '(', ')', ' ', ' '),
                  array(' ', ' ', '(', '>', '.', '<', ')', ' ', ' '),
                  array(''', '(', '"', ')', ' ', '(', '"', ')', '''),
              ),
);

Then I want to echo the bunny’s using the foreach loop:

echo "<pre>";
  foreach($bunnys as $bunny)
    {foreach ($bunny as $row)
      {foreach ($row as $piece) 
      echo "$piece";   
      echo "<br>";
      }
    }
echo "</pre>";

Actual result
The problem is, is that the bunny’s are standing underneath eachother, like this:

() ()
(o.o)
‘(") (")’

() ()
(>.<)
‘(") (")’

Desired result
But what I want, is that the bunny’s standing next to each other, like this:

() () () ()
(o.o) (>.<)
‘(") (")”(") (")’

I already tried to delete the echo "<br>";, but then the result is that there are six lines.

Anybody havind an idea what I am doing wrong?

2

Answers


  1. Try array_merge — Merge one or more arrays

    https://www.php.net/manual/en/function.array-merge.php

    Login or Signup to reply.
  2. $bunnys = array(
                  array( //$bunny
                      array(' ', ' ', '(', ')', ' ', '(', ')', ' ', ' '), //$row en in de array is $piece
                      array(' ', ' ', '(', 'o', '.', 'o', ')', ' ', ' '),
                      array(''', '(', '"', ')', ' ', '(', '"', ')', '''),
                  ),
                  array(
                      array(' ', ' ', '(', ')', ' ', '(', ')', ' ', ' '),
                      array(' ', ' ', '(', '>', '.', '<', ')', ' ', ' '),
                      array(''', '(', '"', ')', ' ', '(', '"', ')', '''),
                  ),
    );
    
    $bunnysArray1 = $bunnys[0];
    $bunnysArray2 = $bunnys[1];
    foreach ($bunnysArray1 as $k=> $v){
    
        echo implode('',$v);
        echo implode('',$bunnysArray2[$k]);
        echo php_sapi_name() == "cli" ? PHP_EOL : '<br>';
    }
    

    output

      () ()    () ()  
      (o.o)    (>.<)  
    '(") (")''(") (")'
    

    see in action
    https://onlinegdb.com/yP0-SxazPR

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search