我有多维数组,如下所示,其中cid作为选项,值被定义为其属性。这种阵列基于从O1,O2,O3等的选择产生。
Array
(
[0] => Array
(
[cid] => O1
[values] => Array
(
[0] => O1A1
[1] => O1A2
)
)
[1] => Array
(
[cid] => O2
[values] => Array
(
[0] => O2A1
[1] => O2A2
[2] => O2A3
)
)
)
现在我需要在HTML表格中生成一个矩阵,如下所示:
+--------+------------+-----------------------------+
| O1 | O2 | Some other columns |
+--------+------------+-----------------------------+
| O1A1 | O2A1 | Some other column values |
| O1A1 | O2A2 | Some other column values |
| O1A1 | O2A3 | Some other column values |
| O1A2 | O2A1 | Some other column values |
| O1A2 | O2A2 | Some other column values |
| O1A2 | O2A3 | Some other column values |
+--------+------------+-----------------------------+
共有6行(创建类似2 X 3(选项O1和O2值))。我试过嵌套的for..loop但是数组有动态值(它基于选择)。
提前感谢您的帮助。
答案 0 :(得分:0)
以下是解决方案:)
<?php
$input = array(0 => array('cid' => 'O1', 'values' => array (0 => 'O1A1',1 => 'O1A2')), 1 => array('cid' => 'O2', 'values' => array (0 => 'O2A1', 1 => 'O2A2', 2=>'O2A3')));
echo "<pre>"; print_r($input);
///make an array of matrix
$matrixArr =array();
for( $i= 0; $i< 2; $i++ )
{
for( $j=0; $j <3; $j++ )
{
$matrixArr[$input[0]['values'][$i]][]= $input[1]['values'][$j];
}
}
echo "<pre>"; print_r($matrixArr);
///now print that array
foreach($matrixArr as $key =>$val)
{
foreach($val as $v)
{
echo $key." | ".$v.' | Some other column values </br>';
}
?>
你会得到这个
O1A1 | O2A1 | Some other column values
O1A1 | O2A2 | Some other column values
O1A1 | O2A3 | Some other column values
O1A2 | O2A1 | Some other column values
O1A2 | O2A2 | Some other column values
O1A2 | O2A3 | Some other column values
答案 1 :(得分:0)
先生,检查一下,如果它对你有帮助......
<?php
$input = array(
0 => array(
'cid' => 'O1',
'values' => array (0 => 'O1A1',1 => 'O1A2', 2=>'01A3',3=>'01A4')),
1 => array(
'cid' => 'O2',
'values' => array (0 => 'O2A1', 1 => 'O2A2', 2=>'O2A3',3=>'O2A4',4=>'O2A5'))
);
$matrixArr =array();
for( $i= 0; $i < count($input[0]['values']); $i++ )
{
for( $j=0; $j < count($input[1]['values']); $j++ )
{
$matrixArr[$input[0]['values'][$i]][]= $input[1]['values'][$j];
}
}
foreach($matrixArr as $key =>$val)
{
foreach($val as $v)
{
echo $key." | ".$v.' | Some other column values </br>';
}
}
?>