Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从<;数组创建网格;部门>';s_Php_Html_Arrays - Fatal编程技术网

Php 从<;数组创建网格;部门>';s

Php 从<;数组创建网格;部门>';s,php,html,arrays,Php,Html,Arrays,我试图编写一个简单的函数,它接受一个项目数组,然后以嵌套的div返回该数组 到目前为止,我所拥有的: // items that need to be nested $arr_items = array('<div>A</div>','<div>B</div>','<div>C</div>','<div>D</div>','<div>E</div>','<div>F

我试图编写一个简单的函数,它接受一个项目数组,然后以嵌套的div返回该数组

到目前为止,我所拥有的:

// items that need to be nested
$arr_items = array('<div>A</div>','<div>B</div>','<div>C</div>','<div>D</div>','<div>E</div>','<div>F</div>','<div>G</div>','<div>H</div>','<div>I</div>','<div>J</div>','<div>K</div>','<div>L</div>');

// build columns of items
$cols_per_row   = 2;
$cols_last      = $cols_per_row + 1;
$column_group_open  = "\n<div class='columns'>\n";
$column_group_close = "</div>\n";
$c_items = count($arr_items);

if ($c_items>0)
{
    $html = $column_group_open;
    $c=0;
    foreach ($arr_items as $item)
    {
        // add item to current colun
        $html .= $item;
        if ($c==$cols_per_row)
        {
            // end current column 
            $html .= $column_group_close;

            // are there more items left?
            if ($c<$c_items)
            {
                // start another column
                $html .= $column_group_open;
            }
        }

        $c++;
    }
    // close last column
    if ($c<$cols_last) {$html .= $column_group_close;}

}
//需要嵌套的项
$arr_items=数组('A','B','C','D','E','F','G','H','I','J','K','L');
//构建项目列
$cols_/行=2;
$cols_last=$cols_每行+1;
$column\u group\u open=“\n\n”;
$column\u group\u close=“\n”;
$c_项目=计数($arr_项目);
如果($c_项>0)
{
$html=$column\u group\u open;
$c=0;
foreach($arr_项目作为$item)
{
//将项添加到当前列
$html.=$item;
如果($c==每行$cols\u)
{
//结束当前列
$html.=$column\u group\u close;
//还有别的东西吗?

如果($c您的代码可以简化

请检查以下内容。您可能需要删除PHP_下线位,以获得语法正确的HTML

// items that need to be nested
$items = ['<div>A</div>','<div>B</div>','<div>C</div>','<div>D</div>','<div>E</div>','<div>F</div>','<div>G</div>','<div>H</div>','<div>I</div>','<div>J</div>','<div>K</div>'];

$item_count = count($items);
$final_html = '';
$temp_container = [];
$columns = 2;

$i = 0;
$j = 1;
$html_1 = "<div class='columns'>";
$html_2 = "</div>";

foreach ($items as $item)
{
    $temp_container[$j] = $item;

    if ($j == $columns OR $i == $item_count-1) {
        $temp_html = '';

        foreach ($temp_container as $temp) {
            $temp_html .= $temp;
        }

        $final_html .= $html_1 . PHP_EOL . $temp_html . $html_2 . PHP_EOL;

        $temp_container = [];
        $j = 0;
    }

    $j++;
    $i++;
}

var_dump($final_html);
//需要嵌套的项
$items=['A','B','C','D','E','F','G','H','I','J','K'];
$item\u count=计数($items);
$final_html='';
$temp_container=[];
$columns=2;
$i=0;
$j=1;
$html_1=“”;
$html_2=“”;
foreach($items作为$item)
{
$temp_container[$j]=$item;
如果($j=$columns或$i==$item\u count-1){
$temp_html='';
foreach($temp_容器作为$temp){
$temp_html.=$temp;
}
$final_html.=$html_1.PHP_EOL.$temp_html.$html_2.PHP_EOL;
$temp_container=[];
$j=0;
}
$j++;
$i++;
}
变量转储($final\u html);

使用PHP的一些内置数组操作函数,您可以在不使用任何循环的情况下执行此操作:

$arr_items    = [
    '<div>A</div>', '<div>B</div>', '<div>C</div>', '<div>D</div>', '<div>E</div>', '<div>F</div>',
    '<div>G</div>', '<div>H</div>', '<div>I</div>', '<div>J</div>', '<div>K</div>', '<div>L</div>'
];
$cols_per_row = 2;
$cg_open      = "\n<div class='columns'>\n";
$cg_close     = "\n</div>\n";

$result  = array_chunk($arr_items, $cols_per_row);
$result2 = array_map(
    function($i) use ($cg_open, $cg_close) {return $cg_open . implode("", $i)  .$cg_close;},
    $result
);
echo implode("\n", $result2);
$arr\u项目=[
‘A’、‘B’、‘C’、‘D’、‘E’、‘F’,
“G”、“H”、“I”、“J”、“K”、“L”
];
$cols_/行=2;
$cg\U open=“\n\n”;
$cg\U close=“\n\n”;
$result=数组块($arr\u items,$cols\u/行);
$result2=数组\u映射(
函数($i)使用($cg_打开,$cg_关闭){返回$cg_打开。内爆(“,$i)。$cg_关闭;},
$result
);
回波内爆(“\n”,$result2);

每当添加
$cols\u每行
列时,使用
%
运算符将
$c
除以
$cols\u每行
以关闭该行后的余数


ie
if($c>0&$c%$cols\u/行==$cols\u/行-1)

而不是
if($c==$cols\u/行)
试试
if($c>0&$c%$cols\u/行==0)
。我认为这两个问题是:数组键从零开始,因此第一次
$c
2
是在第三个而不是第二个元素上;然后
$c
总是大于
$cols\u每行
%
操作符将
$c
除以
$cols\u每行
后返回余数。这更接近了,虽然第一组包含3个div(其余组包含2个div,除了最后一个,预计为1个div)哦,是的,公平点。
如果($c%$cols\u每行=$cols\u每行-1)
?必须重新添加
$c>0
子句,那么这就行了:
如果($c>0&$c%$cols\u每行==$cols\u每行-1)
。请随意删除此连接答案。我希望@MattRaines将其作为答案发布,这就是我使用的答案。
// items that need to be nested
$items = ['<div>A</div>','<div>B</div>','<div>C</div>','<div>D</div>','<div>E</div>','<div>F</div>','<div>G</div>','<div>H</div>','<div>I</div>','<div>J</div>','<div>K</div>'];

$item_count = count($items);
$final_html = '';
$temp_container = [];
$columns = 2;

$i = 0;
$j = 1;
$html_1 = "<div class='columns'>";
$html_2 = "</div>";

foreach ($items as $item)
{
    $temp_container[$j] = $item;

    if ($j == $columns OR $i == $item_count-1) {
        $temp_html = '';

        foreach ($temp_container as $temp) {
            $temp_html .= $temp;
        }

        $final_html .= $html_1 . PHP_EOL . $temp_html . $html_2 . PHP_EOL;

        $temp_container = [];
        $j = 0;
    }

    $j++;
    $i++;
}

var_dump($final_html);
$arr_items    = [
    '<div>A</div>', '<div>B</div>', '<div>C</div>', '<div>D</div>', '<div>E</div>', '<div>F</div>',
    '<div>G</div>', '<div>H</div>', '<div>I</div>', '<div>J</div>', '<div>K</div>', '<div>L</div>'
];
$cols_per_row = 2;
$cg_open      = "\n<div class='columns'>\n";
$cg_close     = "\n</div>\n";

$result  = array_chunk($arr_items, $cols_per_row);
$result2 = array_map(
    function($i) use ($cg_open, $cg_close) {return $cg_open . implode("", $i)  .$cg_close;},
    $result
);
echo implode("\n", $result2);