Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #11604

Re: splitting list into columns

Message-ID <2504041.36zPCE0XX7@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-10-28 01:02 +0100
Subject Re: splitting list into columns
Newsgroups comp.lang.php
References <powohrz248gd.1bjaj4r4el9yc$.dlg@40tude.net> <52670aad$0$9522$9b4e6d93@newsspool1.arcor-online.net> <1457114.2ZHQPtZS3d@PointedEars.de> <526d4270$0$9517$9b4e6d93@newsspool1.arcor-online.net>

Show all headers | View raw


Christoph Michael Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Christoph Michael Becker wrote:
>>> To create a table from a 1-dimensional indexed array $values in N-order,
>>> you may use the following outline:
>>>
>>> [loop]
>> 
>> Or you could use
>> 
>>   echo '<table>'
>>     . '<tr>'
>>     . implode('</tr><tr>',
>>         array_map(
>>           function ($e) {
>>             return '<td>' . implode('</td><td>', $e) . '</td>';
>>           },
>>           array_chunk($values, $cols)
>>         )
>>       )
>>     . '</tr>'
>>     . '</table>';
>> 
>> and additionally use array_slice() if you wanted to limit the output to
>> $rows rows :)
> 
> That is a nice solution (the applicative programming paradigm is so
> elegant; unfortunately, I'm still not much used to it). :)
> 
> However, this algorithm will emit the table in Z-order.  I wonder if
> there is an equally elegant solution for N-order.

I presume you mean this:

  /**
   * Returns the transpose of a two-dimensional array.
   * 
   * NOTE: Does not preserve the indexes.
   * 
   * @author Codler
   * @param array $a
   * @return array
   * @see http://stackoverflow.com/a/3423692/855543
   */
  function array_transpose ($a)
  {
    array_unshift($a, null);
    return call_user_func_array('array_map', $a);
  }

  echo '<table>'
    . '<tr>'
    . implode('</tr><tr>',
        array_map(
          function ($e) {
            return '<td>' . implode('</td><td>', $e) . '</td>';
          },
          array_transpose(array_chunk($values, $rows))
        )
      )
    . '</tr>'
    . '</table>';

You can additionally use array_map('array_slice', …) to limit the number of 
columns :)

>> See also:
>> 
>> <http://php.net/array_chunk>
>> 
>> (the exploit or false entry apparently has disappeared; Chromium no
>> longer warns about php.net)
> 
> Some explanation about what has happened can be found on
> <http://php.net/archive/2013.php#id2013-10-24-2>.

Thanks.


PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

splitting list into columns richard <noreply@example.com> - 2013-10-22 18:19 -0400
  Re: splitting list into columns Christoph Michael Becker <cmbecker69@arcor.de> - 2013-10-23 01:31 +0200
    Re: splitting list into columns richard <noreply@example.com> - 2013-10-22 20:53 -0400
      Re: splitting list into columns Evan Platt <evan@theobvious.espphotography.com.invalid> - 2013-10-22 18:40 -0700
    Re: splitting list into columns Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-26 03:04 +0200
      Re: splitting list into columns Christoph Michael Becker <cmbecker69@arcor.de> - 2013-10-27 17:42 +0100
        Re: splitting list into columns Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-28 01:02 +0100
          Re: splitting list into columns Christoph Michael Becker <cmbecker69@arcor.de> - 2013-10-30 01:34 +0100
  Re: splitting list into columns Arno Welzel <usenet@arnowelzel.de> - 2013-10-23 23:24 +0200
    Re: splitting list into columns Frank Thomason <Frank@example.net> - 2013-10-24 08:35 -0400
      Re: splitting list into columns Jerry Stuckle <jstucklex@attglobal.net> - 2013-10-24 09:01 -0400
        Re: splitting list into columns Frank Thomason <Frank@example.net> - 2013-10-24 10:05 -0400
  Re: splitting list into columns Biju S <biju@pixnnova.com> - 2013-10-29 22:38 -0700

csiph-web