我有一个像这样的数组结构,它来自一个循环:
$a = array('9-aug','$50','room1');
$b = array('10-aug','$60','room1');
$c = array('9-aug','$70','room2');
$d = array('10-aug','$80','room2');
我需要得到这样的数据(即)根据日期对数据进行分组:
['9-aug-2014']=>{
[0]=>{'$50','room1'},
[1]=>{'$70','room2'}
}
['10-aug-2014']=>{
[0]=>{'$60','room1'},
[1]=>{'$80','room2'}
}
这有什么功能吗?我尝试过使用array_map,但无法获得所需的o / p。 这是我尝试过的:
$e = array_map(null, $a, $b, $c,$d);
print_r($e);
得到了:
Array
(
[0] => Array
(
[0] => 9-aug
[1] => 10-aug
[2] => 9-aug
[3] => 10-aug
)
[1] => Array
(
[0] => $50
[1] => $60
[2] => $70
[3] => $80
)
[2] => Array
(
[0] => room1
[1] => room1
[2] => room2
[3] => room2
)
)
请帮助。我无法格式化$a,$b,$c,$d
数组,因为这是获取数据的方式。
如何格式化数据,以便以此格式显示。
<table class="price-breakdown">
<thead>
<!--<tr><th></th><th style="font-weight:normal" colspan="3">Price per room (inc VAT & taxes)</th></tr>-->
<tr>
<th class="date-col">Date</th>
<th class="date-col">Room</th>
<th valign="top">
Premier Saver
<br>
<span class="pbThSubtitle">
Price per room<br>(inc VAT & taxes)
</span>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<nobr>Wed 17 Sep 14</nobr>
</td>
<td align="left">1</td>
<td>
£<span>71.00</span>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">2</td>
<td>
£<span>71.00</span>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">3</td>
<td>
£<span>71.00</span>
</td>
</tr>
<tr>
<td>
<nobr>Thu 18 Sep 14</nobr>
</td>
<td align="left">1</td>
<td>
£<span>67.00</span>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">2</td>
<td>
£<span>67.00</span>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">3</td>
<td>
£<span>67.00</span>
</td>
</tr>
<tr>
<td>
<nobr>Fri 19 Sep 14</nobr>
</td>
<td align="left">1</td>
<td>
£<span>83.00</span>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">2</td>
<td>
£<span>83.00</span>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">3</td>
<td>
£<span>83.00</span>
</td>
</tr>
</tbody>
<tfoot>
<tr class="yellow-top">
<td colspan="5"></td>
</tr>
<tr>
<td class="date-col">
<nobr>Total 3 nights </nobr>
</td>
<td>3 rooms</td>
<td> £<span>663.00</span>
</td>
<td> £<span>744.00</span>
</td>
<td> £<span>822.75</span>
</td>
</tr><tr class="yellow-btm"><td colspan="5"></td></tr></tfoot>
</table>
答案 0 :(得分:0)
这可以通过以下代码完成。以下网址提供了工作脚本:http://sugunan.net/demo/array4.php
$a = array('9-aug','$50','room1');
$b = array('10-aug','$60','room1');
$c = array('9-aug','$70','room2');
$d = array('10-aug','$80','room2');
$arr = array($a,$b,$c,$d);
$arr_merge = array();
foreach($arr as $key_dim1=>$val_dim1)
{
$arr_merge[$val_dim1[0]][] = array($val_dim1[1],$val_dim1[2]);
}
print_r($arr_merge);
数组中的表创建部分
<table class="price-breakdown">
<thead>
<tr>
<th class="date-col">Date</th>
<th class="date-col">Room</th>
<th valign="top">Premier Saver<br>
<span class="pbThSubtitle">Price per room<br>(inc VAT & taxes)</span>
</th>
</tr>
</thead>
<tbody>
<?php
foreach($arr_merge as $key1=>$val1){
foreach($val1 as $key2=>$val2){
?>
<tr>
<td><nobr><?php if($key2==0){ echo $key1; } ?></nobr></td>
<td align="left"><?php echo $val2[1]; ?></td>
<td><?php echo $val2[0]; ?></td>
</tr>
<?php }} ?>
</tbody>
</table>
答案 1 :(得分:0)
您的数据源包含重复的密钥 - 我不知道这是故意还是仅填写数据。
如果键是唯一的,这是一个可以处理它的函数:
<?php
$a = array('9-aug','$50','room1');
$b = array('10-aug','$60','room1');
$c = array('9-sept','$70','room2');
$d = array('10-sept','$80','room2');
$final = firstKeyAsKey(array($a, $b, $c, $d));
function firstKeyAsKey(array $joined) {
$cleanedArr = array();
foreach($joined as $key => $arr) {
if(is_array($arr) && isset($arr[0])) {
$outerKey = $arr[0];
unset($arr[0]);
$cleanedArr[$outerKey] = $arr;
}
}
return $cleanedArr;
}
// dump result
var_dump($final);
?>
同时强>
如果您确实希望嵌套数组成为对象,请添加此补丁:
<?php
$cleanedArr[$outerKey] = json_decode(json_encode($arr), FALSE);
?>
答案 2 :(得分:-1)
这是我得到的。另一个答案是类似的。这将完全返回您的请求。
$a = array('9-aug','$50','room1');
$b = array('10-aug','$60','room1');
$c = array('9-aug','$70','room2');
$d = array('10-aug','$80','room2');
$fullArray[] =$a;
$fullArray[] =$b;
$fullArray[] =$c;
$fullArray[] =$d;
function makeNewArray($array){
foreach($array as $x){
$newarray[$x[0]][]="{'".$x[1]."','".$x[2]."'}";
}
return $newarray;
}
$result= makeNewArray($fullArray);
print_r($result);