FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » splitting list into columns
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: splitting list into columns [message #183468 is a reply to message #183462] Mon, 28 October 2013 00:02 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
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(at)news(dot)demon(dot)co(dot)uk> (2004)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Unzip password-protected ZIP file in RAM?
Next Topic: PDO - Cannot retrieve warnings with emulated prepares disabled
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Sep 19 22:23:05 GMT 2024

Total time taken to generate the page: 0.31570 seconds