需要一些帮助正确格式化此数组:
这是数组的转储:
array (size=5)
'Bank Deposit' =>
array (size=13)
'Payment Method' => string 'Bank Deposit' (length=12)
0 => string 'Bank Deposit' (length=12)
201704 => string '1200' (length=4)
201705 => string '0' (length=1)
201701 => string '0' (length=1)
201706 => string '0' (length=1)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '0' (length=1)
201709 => string '0' (length=1)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Cash' =>
array (size=13)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '300' (length=3)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
201602 => string '12' (length=2)
201709 => string '12' (length=2)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Cheque' =>
array (size=13)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '231' (length=3)
201709 => string '231' (length=3)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Mobile Money' =>
array (size=13)
'Payment Method' => string 'Mobile Money' (length=12)
0 => string 'Mobile Money' (length=12)
201704 => string '0' (length=1)
201705 => string '0' (length=1)
201701 => string '0' (length=1)
201706 => string '0' (length=1)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '0' (length=1)
201709 => string '0' (length=1)
201702 => string '150' (length=3)
201710 => string '150' (length=3)
'' =>
array (size=13)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
201602 => string '243' (length=3)
201709 => string '243' (length=3)
201702 => string '150' (length=3)
201710 => string '150' (length=3)
问题:如何摆脱阵列中的“付款方式”和0键?
我做了什么:
拼接偏移量为1的值:https://ibb.co/fogNTk
拼接偏移量为1的值和键:https://ibb.co/jgU98k
这将删除列,但随后重新索引键!
<div class='box-body no-padding'>
<table class='table cell-border' id='summary'><thead>
<tr>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
} foreach (array_splice($v,1) as $k => $v){
echo '<th>'.$k.'</th>';
}
var_dump($temp);
}
?>
</tr></thead>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
foreach (array_splice($v , 1) as $k => $v){
echo '<td>'.$v.'</td>';
}
echo "</tr>";
}
?>
</table>
</div>
</div>
</div>
我之前发过帖子,但似乎没有达到目的,但如果你需要任何额外的帮助来获得图片,你可以在这里看到更详细的问题:Multidimensional Array Dynamic Columns Format。感谢
答案 0 :(得分:1)
<?php
$data = [
'foo' =>
[
'unwanted' => 'something',
0 => 'something else not wanted',
'201704' => '23',
],
'bar' =>
[
'unwanted' => 'something',
0 => 'something else not wanted',
'201703' => '47',
]
];
foreach($data as $k => $v) {
unset($data[$k]['unwanted']);
unset($data[$k][0]);
}
var_export($data);
输出:
array (
'foo' =>
array (
201704 => '23',
),
'bar' =>
array (
201703 => '47',
),
)