我在这里有一个问题......
我有一个像mysql这样的数组...
array(3) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["name"]=>
string(8) "Delivery"
["namaShipp"]=>
string(3) "JNE"
["price"]=>
string(5) "30000"
}
[1]=>
array(4) {
["id"]=>
string(1) "3"
["name"]=>
string(12) "Installation"
["namaShipp"]=>
string(7) "Kudamas"
["price"]=>
string(1) "0"
}
[2]=>
array(4) {
["id"]=>
string(1) "5"
["name"]=>
string(12) "Installation"
["namaShipp"]=>
string(5) "MTECH"
["price"]=>
string(1) "0"
}
}
然后我想把它输出到一个简单的表..我不知道如何.. 因此结果按['name']键分组。 我的问题是如何输出该数组就像这样
-----------------------------
INSTALLATION (get from ['name'])
----------------------------
MTECH - 0 <---- MTECH is get from ['namaShipp'] and 0 is from ['price']
KUDAMAS - 0
-----------------------------
DELIVERY
-----------------------------
JNE - 30000
任何人都可以帮忙吗?我会很感激.. 我对这件事感到很困惑......
之前感谢
答案 0 :(得分:0)
这样的事情:
$current=false;
foreach($result as $item)
{
if($item['name']!==$current)
{
echo '
---------------------
'.$item['name'].'
---------------------';
$current=$item['name'];
}
echo '
'$item['namaShipp'].' - '.$item['price'];
}
答案 1 :(得分:0)
试试这个(假设你展示的阵列是$processedArray
):
$processedArray = array();
foreach ($mainArray as $innerArray) {
$processedArray[$innerArray['name']][] = $innerArray;
}
foreach ($processedArray as $name => $itemsArray) {
echo '----<br />'.$name.'<br />---';
foreach ($itemsArray as $item) {
echo $item['namaShipp'].' - '.$item['price'].'<br />';
}
}
答案 2 :(得分:0)
转换你的阵列!创建一个记录数组数组:
$list=array();
foreach($data as $item)
$list[$item['name']][]=$item;
foreach($list as $groupname=>$records){
echo '--------------- '.$groupname.' ------------';
foreach($records as $item){
//output one $item as before...
}
}