希望我能正确地问这个问题。 我有一个多维数组,如下所示,这已被[类型]预先排序,现在我需要遍历它,找到匹配的类型,如果找到,我需要输出以下(对不起,短输入,但真的不能用文字解释。
Array
(
[0] => Array
(
[image] => 479/beatniq/devt4147205-7.jpg
[types] => BE1
[sizes] => 43
[prices] => 12000000
[quantity] => 1
[aspect] => South
[floor] => 16
[beds] => 1
)
[1] => Array
(
[image] => 479/beatniq/devt4147205-7.jpg
[types] => BE1
[sizes] => 44
[prices] => 12003060
[quantity] => 1
[aspect] => South
[floor] => 16
[beds] => 1
)
[2] => Array
(
[image] => 479/beatniq/devt4147205-7.jpg
[types] => BE1
[sizes] => 48
[prices] => 12009800
[quantity] => 1
[aspect] => North
[floor] => 24
[beds] => 1
)
[3] => Array
(
[image] => 479/beatniq/devt4147205-4.jpg
[types] => BE2
[sizes] => 79
[prices] => 22046511
[quantity] => 1
[aspect] => East
[floor] => 32
[beds] => 2
)
[4] => Array
(
[image] => 479/beatniq/devt4147205-1.jpg
[types] => BE2P
[sizes] => 108
[prices] => 30139534
[quantity] => 1
[aspect] => South
[floor] => 39
[beds] => 2
)
[5] => Array
(
[image] => 479/beatniq/devt4147205-1.jpg
[types] => BE2P
[sizes] => 110
[prices] => 30141534
[quantity] => 1
[aspect] => South
[floor] => 34
[beds] => 2
)
)
输出:
Type of property Sizes Price range Availability
BE1 43-44m2 ฿12,000,000-฿12,009,800 3
BE2 79m2 ฿22,046,511 1
BE2P 108-110m2 ฿30,139,534-฿30,141,534 2
我知道以下内容不会达到结果,但是我到目前为止单独输出数组,在哪里需要分组?
if( ! empty ( $dev_units_available ) ) {
$arraySize = count($dev_units_available);
for($i=0;$i<$arraySize;$i++){ ?>
<tr class="highlight">
<td style="width:60px;">
<a href="/a-link/"><img src="<?php echo $dev_units_available[$i][image];?>" width="100" class="masthead" alt="a building"></a>
</td>
<td><?php echo $dev_units_available[$i][types];?></td>
<td><?php echo $dev_units_available[$i][sizes].' '.$areaunit;?></td>
<td><?php echo get_custom_price($dev_units_available[$i][prices]);?></td>
<td><strong><?php echo $dev_units_available[$i][quantity];?> available</strong></td>
<td class="int" style="width:120px;"><div class="button"><a href="#" onclick="toggleRow(<?php echo $i;?>);return false;" class="add"><strong>More details</strong></a></div></td>
</tr>
<tr id=row<?php echo $i;?> style='height:0px;'>
<td style='padding:0;border:none;'>
<div id="div<?php echo $i;?>" style="display:none;">Hidden row content goes here</div>
</td>
</tr>
}
}
非常感谢,Malisa
答案 0 :(得分:1)
经过大量的试验和错误后,我们会对它进行排序。毫无疑问,有一种更简单的方法,但是这样做有效。
只需按[types]
对原始数组进行排序Array
(
[0] => Array
(
[image] => /479/beatniq/devt4147205-7.jpg
[types] => BE1
[sizes] => 45
[prices] => 12000000
[quantity] => 1
[aspect] =>
[floor] => 23
[beds] => 1
)
[1] => Array
(
[image] => /479/beatniq/devt4147205-2.jpg
[types] => BE2
[sizes] => 53
[prices] => 15667000
[quantity] => 1
[aspect] =>
[floor] => 45
[beds] => 2
)
[2] => Array
(
[image] => /479/beatniq/devt4147205-7.jpg
[types] => BE1
[sizes] => 48
[prices] => 13890000
[quantity] => 1
[aspect] =>
[floor] => 56
[beds] => 1
)
)
使用以下功能:
function arraySort($input,$sortkey){
foreach ($input as $key=>$val) $output[$val[$sortkey]][]=$val;
return $output;
}
$dev_units_available = arraySort($dev_units_available,'types');
这给了我这个数组:
Array
(
[BE1] => Array
(
[0] => Array
(
[image] =>/479/beatniq/devt4147205-7.jpg
[types] => BE1
[sizes] => 45
[prices] => 12000000
[quantity] => 1
[aspect] =>
[floor] => 23
[beds] => 1
)
[1] => Array
(
[image] =>/479/beatniq/devt4147205-7.jpg
[types] => BE1
[sizes] => 48
[prices] => 13890000
[quantity] => 1
[aspect] =>
[floor] => 56
[beds] => 1
)
)
[BE2] => Array
(
[0] => Array
(
[image] => /479/beatniq/devt4147205-2.jpg
[types] => BE2
[sizes] => 53
[prices] => 15667000
[quantity] => 1
[aspect] =>
[floor] => 45
[beds] => 2
)
)
)
然后通过将原始循环更改为此来输出。
foreach($dev_units_available as $item){
$arraySize = count($item);
for($i=0;$i<$arraySize;$i++){
HTML HERE
}
}