我有一个动态数组如下:
Array
(
[0] => stdClass Object
(
[student_id] => 8
[student_name] => A
[student_class] => 2
[student_status] => 1
[class_id] => 2
[class_name] => C1
[class_status] => 1
)
[1] => stdClass Object
(
[student_id] => 10
[student_name] => B
[student_class] => 2
[student_status] => 1
[class_id] => 2
[class_name] => C1
[class_status] => 1
)
[2] => stdClass Object
(
[student_id] => 9
[student_name] => C
[student_class] => 2
[student_status] => 1
[class_id] => 2
[class_name] => C1
[class_status] => 1
)
[3] => stdClass Object
(
[student_id] => 11
[student_name] => D
[student_class] => 2
[student_status] => 1
[class_id] => 2
[class_name] => C1
[class_status] => 1
)
[4] => stdClass Object
(
[student_id] => 4
[student_name] => E
[student_class] => 1
[student_status] => 1
[class_id] => 1
[class_name] => C2
[class_status] => 1
)
[5] => stdClass Object
(
[student_id] => 6
[student_name] => F
[student_class] => 1
[student_status] => 1
[class_id] => 1
[class_name] => C2
[class_status] => 1
)
[6] => stdClass Object
(
[student_id] => 5
[student_name] => G
[student_class] => 1
[student_status] => 1
[class_id] => 1
[class_name] => C2
[class_status] => 1
)
[7] => stdClass Object
(
[student_id] => 7
[student_name] => H
[student_class] => 1
[student_status] => 1
[class_id] => 1
[class_name] => C2
[class_status] => 1
)
[8] => stdClass Object
(
[student_id] => 7
[student_name] => I
[student_class] => 1
[student_status] => 1
[class_id] => 1
[class_name] => C2
[class_status] => 1
)
[9] => stdClass Object
(
[student_id] => 7
[student_name] => J
[student_class] => 1
[student_status] => 1
[class_id] => 1
[class_name] => C2
[class_status] => 1
)
)
我想要的是,使用PHP格式化如下
<div class="main_wrap">
<h2>C1</h2>
<div class="inner_wrap">
<p>
<span>A</span>
</p>
<p>
<span>B</span>
</p>
<p>
<span>C</span>
</p>
</div>
<div class="inner_wrap">
<p>
<span>D</span>
</p>
</div>
</div>
<div class="main_wrap">
<h2>C2</h2>
<div class="inner_wrap">
<p>
<span>E</span>
</p>
<p>
<span>F</span>
</p>
<p>
<span>G</span>
</p>
</div>
<div class="inner_wrap">
<p>
<span>H</span>
</p>
<p>
<span>I</span>
</p>
<p>
<span>J</span>
</p>
</div>
</div>
我知道如何使用foreach循环它,但不知道如何动态地使用div包装。
答案 0 :(得分:1)
看看这是否是你要找的。我的解决方案使用存储阵列,array_chunk()
将子数组拆分为3组,然后implode()
创建html:
// Storage array
$sections = array();
// Loop through your large array (I am calling it "all")
foreach($all as $rows) {
// Store the class title
$cName = $rows->class_name;
// Store name (and whatever else you want)
// You could store the key for the original object
// so you can reference back on the next loop below
$sections[$cName][] = $rows->student_name;
}
// Loop through the store
foreach($sections as $key => $values) {
// Chunk down to whatever grouping you want (3 is what you mention)
$grp = array_chunk($values,3);
// Use implode for the chunks
echo '<div class="main_wrap">'.PHP_EOL.'<h2>'.$key.'</h2>'.PHP_EOL;
foreach($grp as $values) {
echo '<div class="inner_wrap">'.PHP_EOL;
echo "\t<p>".PHP_EOL."\t\t<span>".implode("</span>".PHP_EOL."\t</p>".PHP_EOL."\t<p>".PHP_EOL."\t\t<span>",$values)."</span>".PHP_EOL."\t</p>".PHP_EOL."</div>".PHP_EOL;
}
echo '</div>'.PHP_EOL;
}
应该给你(或者至少它给我):
<div class="main_wrap">
<h2>C1</h2>
<div class="inner_wrap">
<p>
<span>A</span>
</p>
<p>
<span>B</span>
</p>
<p>
<span>C</span>
</p>
</div>
<div class="inner_wrap">
<p>
<span>D</span>
</p>
</div>
</div>
<div class="main_wrap">
<h2>C2</h2>
<div class="inner_wrap">
<p>
<span>E</span>
</p>
<p>
<span>F</span>
</p>
<p>
<span>G</span>
</p>
</div>
<div class="inner_wrap">
<p>
<span>H</span>
</p>
<p>
<span>I</span>
</p>
<p>
<span>J</span>
</p>
</div>
</div>
注意,您可以使用格式化,但它应该是相同的布局,但是按照标签,您必须添加一对。
还注意:我没有进行任何检查(例如
empty()
),所以如果对象没有填充或者你有什么,那么会有foreach()
警告。
答案 1 :(得分:0)
根据我的理解,您需要在节点之间创建父子关系,如下所示
$book = array();
$book[0] = new stdClass;
$book[0]->id = "1";
$book[0]->student_name = "A";
$book[0]->parent_id = "0";
$book[1] = new stdClass;
$book[1]->id = "2";
$book[1]->student_name = "B";
$book[1]->parent_id = "1";
然后为此创建树结构数组并根据需要进行渲染。
Array
(
[0] => Array
(
[id] => 1
[student_name] => A
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 2
[student_name] => B
[parent_id] => 1
[children] => Array
(
)
[data] => stdClass Object
(
)
)
)
[data] => stdClass Object
(
)
)
[2] => Array
(
[id] => 3
[student_name] => C
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[student_name] => D
[parent_id] => 3
[children] => Array
(
)
[data] => stdClass Object
(
)
)
)
[data] => stdClass Object
(
)
)
)