Php 重新排列数组
数组开始时如下所示:Php 重新排列数组,php,arrays,Php,Arrays,数组开始时如下所示: Array ( [SMART Board] => Array ( [0] => sb1 [1] => sb2 [2] => sb3 ) [Projector] => Array ( [0] => pr1 [1] => pr2 [2] => pr3 ) [Speakers] => Array ( [0] => sp1 [1] => sp2 [2] => sp3 ) [Splitte
Array (
[SMART Board] => Array ( [0] => sb1 [1] => sb2 [2] => sb3 )
[Projector] => Array ( [0] => pr1 [1] => pr2 [2] => pr3 )
[Speakers] => Array ( [0] => sp1 [1] => sp2 [2] => sp3 )
[Splitter] => Array ( [0] => spl1 [1] => spl2 [2] => spl3 )
[Wireless Slate] => Array ( [0] => ws1 [1] => ws2 [2] => ws3 )
)
Array (
[0] => sb1
[1] => sb2
[2] => sb3
[3] => pr1
[4] => pr2
[5] => pr3
[6] => sp1
[7] => sp2
[8] => sp3
[9] => spl1
[10] => spl2
[11] => spl3
[12] => ws1
[13] => ws2
[14] => ws3
)
这些键用作我的表列标题。它们各自的数组将承载列信息
我使用array\u slice
和array\u merge\u recursive
将其进一步拆分,使其看起来像两个数组一样漂亮-1个数组包含列名,另一个看起来像这样:
Array (
[SMART Board] => Array ( [0] => sb1 [1] => sb2 [2] => sb3 )
[Projector] => Array ( [0] => pr1 [1] => pr2 [2] => pr3 )
[Speakers] => Array ( [0] => sp1 [1] => sp2 [2] => sp3 )
[Splitter] => Array ( [0] => spl1 [1] => spl2 [2] => spl3 )
[Wireless Slate] => Array ( [0] => ws1 [1] => ws2 [2] => ws3 )
)
Array (
[0] => sb1
[1] => sb2
[2] => sb3
[3] => pr1
[4] => pr2
[5] => pr3
[6] => sp1
[7] => sp2
[8] => sp3
[9] => spl1
[10] => spl2
[11] => spl3
[12] => ws1
[13] => ws2
[14] => ws3
)
然而,当试图写表时,我得到了键0、1、2作为列数据,然后是换行符,然后是3、4、5,然后是换行符。。。等等
我需要它是0,3,6,9,12行中断1,4,7,10,13行中断等
我如何重新排列数组以实现这一点,或者重写数据进入表的方式,以便正确的信息与相应的列对齐
foreach(unserialize($dl->data) as $data){
//first 3 are specialinstructions, system, and room
$uniques = array_slice($data,0,3);
$rows = array_slice($data,3);
$rows2 = array_merge_recursive($rows2, $rows);
//get the specialinstructions, system, and room
foreach($uniques as $unique){
echo $unique."<br/>";
}///foreach uniques
echo "<br>";
}///foreach unserialized
$numberofrooms = count($rows2[key($rows2)]);
$numberofproducts = count($rows2);
print_r($rows2);
unset($rows);
//write the individual rows
foreach($rows2 as $header=>$rowset){
$headers[] = $header;
foreach($rowset as $row){
$rows[] = $row;
}//foreach rowset
}//foreach rows2
echo "<p>";
print_r($rows);
echo '<table class="data-table">
<caption>DL</caption>
<thead><tr>';
foreach($headers as $header){
echo "<th>".$header."</th>";
}
echo '</tr></thead>';
echo '<tbody>';
$i = 0;
foreach($rows as $row){
if($i == 3 || $i == 0){
echo "<tr>";
$i = 1;
}
echo '<td>'.$row.'</td>';
if($i == 2){
echo "</tr>";
}
$i++;
}
echo '</tbody></table>';
foreach(将($dl->data)取消序列化为$data){
//前三个是特殊结构、系统和房间
$uniques=array_slice($data,0,3);
$rows=数组×切片($data,3);
$rows2=数组\合并\递归($rows2,$rows);
//获取特殊结构、系统和房间
foreach($uniques作为$unique){
echo$unique。“
”;
}///foreach uniques
回声“
”;
}///foreach未序列化
$numberofrooms=计数($rows2[键($rows2)]);
$numberofproducts=计数($rows2);
打印(第2行);
未设置($行);
//写下每一行
foreach($rows2作为$header=>$rowset){
$headers[]=$header;
foreach($rowset为$row){
$rows[]=$row;
}//foreach行集
}//foreach rows2
回声“”;
打印(行);
回声'
DL
';
foreach($headers作为$header){
回显“$header.”;
}
回声';
回声';
$i=0;
foreach($行作为$行){
如果($i==3 | |$i==0){
回声“;
$i=1;
}
回显“.$row.”;
如果($i==2){
回声“;
}
$i++;
}
回声';
您可以使用原始数组和以下代码来显示所需的方式:
$i=0;
while($i<3){ // 3 is the number of items in each array
foreach($originalArray as $arr){
print($arr[$i].'<br/>');
}
print('<hr/>');
$i++;
}
$i=0;
虽然($iAs提示:在我看来,如果你不echo
HTML,那么就更容易掌握代码的结构。这样做,例如:
谢谢Felix,我通常会这么做。不过这是一个函数,我觉得它更清晰,可以准确地看到我输出的内容。太好了!稍微修改一下以适应我的脚本和宾果游戏,我们有一个赢家。非常感谢zaf!(我不知道如何在评论中编写php代码,我表示歉意)如果你觉得这很有用,那么你应该单击向上箭头。一次就可以了;)啊,我终于得到了它!我把它给了你,伙计。珍惜它。