我有这样一个数组:
<?php
$biaya=odbc_exec($koneksi,"select * from example");
$no=0;
while(odbc_fetch_row($biaya)){
$no++;
$sub_title=odbc_result($biaya,"subtitle");
$title=odbc_result($biaya,"title");
}
?>如果我显示循环,它将是这样的:

我想根据字幕来分割数组。我想得到这样的排列:


我该如何解决这个问题?
发布于 2017-12-18 07:28:58
Eloquent或Doctrine,而不是运行原始查询。order by。
从示例按字幕顺序选择*while循环中,请使用以下技巧。```javascript$temp =空;
$result = [];
时间(odbc_fetch_row($biaya)){
$no++;
$sub_title=odbc_result($biaya,“字幕”);
$title=odbc_result($biaya,"title");
if($temp != $sub_title) {
$result[$sub_title] = ["no" => $no, "subtitle" => $sub_title, "title" => $title]; $temp = $sub_title;}
否则{
$result[$sub_title][] = ["no" => $no, "subtitle" => $sub_title, "title" => $title];}
}
发布于 2017-12-18 07:23:35
你可以这样做:-
$result = [];
foreach ($array as $row){
$result[$row['subtitle']][] = $row;
}发布于 2017-12-18 07:31:36
这是解决办法..。您可以看到结果这里(更新)
我已经更新了Inventaris类别
<?php
$array=array(
array("no"=>"1","subtitle"=>"Perbekalan", "title"=>"lombok ijo"),
array("no"=>"11","subtitle"=>"Perbekalan", "title"=>"lombok asdf"),
array("no"=>"2","subtitle"=>"Perbekalan", "title"=>"bawang abang"),
array("no"=>"3","subtitle"=>"Inventaris", "title"=>"Wajan")
);
echo "<pre>";
//print_r($array);
$Perbekalan=array();
$Inventaris=array();
$i=0;
foreach ($array as $value) {
if($value['subtitle']=="Perbekalan")
{
$Perbekalan[$i]['no']=$value['no'];
$Perbekalan[$i]['subtitle']=$value['subtitle'];
$Perbekalan[$i]['title']=$value['title'];
}
if($value['subtitle']=="Inventaris")
{
$Inventaris[$i]['no']=$value['no'];
$Inventaris[$i]['subtitle']=$value['subtitle'];
$Inventaris[$i]['title']=$value['title'];
}
$i++;
}
echo "per";
print_r($Perbekalan);
echo "ins";
print_r($Inventaris);
?>https://stackoverflow.com/questions/47863718
复制相似问题