2

I'm having a brain-block with this piece of code. I want to print a multidimensional array in PHP where the 'columns' are separated by hyphens and the 'rows' are separated by newlines.

My problem is that my code prints a '-' at the end of each row. This is obvious when looking at my code:

N.B. ($result is a 2D array i.e. $result[][])

foreach($result as $row){
    foreach($row as $column){
            echo $column . "-";
    }
    echo "\n";
}

This is the output:

42-1-1394752507-16.125-
43-1-1394752569-16.125-
44-1-1394752632-16.562-
45-1-1394752693-17.125-

What is the best way to print this out to avoid the trailing - on each row? I could do a check to see if the current $column is the last element in the $row but I don't actually know how to do this

6 Answers 6

7

implode is your friend:

foreach($result as $row){
    echo implode('-', $row) . "\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly, and is even simpler than what I thought I needed to do. Thanks! Will accept in 5 mins when it lets me
@exantas: I always look for a built-in function, especially to not loop :-)
1
<?php

$row = [
    '42' => [1, 2, 3],
    '43' => [2, 3, 4],
];

array_walk($row, function($item, $key) {
    echo $key . implode('-', $item) . PHP_EOL;
});

Comments

1

I could do a check to see if the current $column is the last element in the $row but I don't actually know how to do this

implode() is the way to go, but if you want to understand how to find out if the current column is the last one:

// foreach (value)
foreach($result as $row){
  $column_count = count($row);

  // for each (key => value)
  foreach($row as $column_index => $column){
    echo $column;

    if($column_index < $column_count - 1){
      echo "-";
  }
  echo "\n";
}

1 Comment

I liked this answer as well. Thanks for the info
1

Instead of the inner foreach you could just use implode:

implode($row,"-");

4 Comments

You don't want to implode the $column it's not an array. And your args are backwards.
@AbraCadaver: For "historical reasons", you can swap the order of parameters. From the docs: implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments. php.net/implode
@RocketHazmat: Doh, its been so long since I looked at the implode manual, but that memory smacks me in the head now.
@AbraCadaver: Of course you are right. I edited my answer according the first parameter. PHP.net also states that this order of the arguments should not be used
0

one option:

foreach($result as $row){
$c='';
    foreach($row as $column){
            $c.= $column . "-";
    }
    $c=rtrim($c,'-');
    echo "$c\n";
}

Comments

0

if you also want to avoid a trailing newline, you could use this one-liner:

echo implode(array_map(function($row) {return implode('-', $row);}, $result), "\n");

of course, oneliners are ugly and not clean code.

what does this code do? it uses implode on each row, and array_map creates a flat (1-dimensional) array of already imploded rows. that is again imploded with newline.

finally, this solution is not the most performant one. but it's a neat oneliner :)

1 Comment

that is not a one liner. the rules say that a one-liner cannot exceed 3 function calls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.