2017-12-18 74 views
1

分裂阵列我有这样的数组:如何在PHP

<?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"); 
    } 
?> 

如果我显示的循环将是这样的:

enter image description here

我想分割基于阵列在小标题上。我想数组是这样的:

enter image description here

enter image description here

我怎样才能解决这个问题?

+0

相反cretainf两个单独数组变量的这更好: - '$阵列=阵列(0 =>数组( 阵列(没有= “1”,字幕= “Perbekalan”, title =“lombok ijo”), array(no =“2”,subtitle =“Perbekalan”,title =“bawang abang”)),1 => array( array(no =“3”,subtitle =“Inventaris “,title =”Wajan“)) );'。你不这么认为吗?还有你如何创建'$ array'?该代码部分丢失。因此,添加 –

+0

array_slice将是有用的\ – TarangP

+0

试试这个http://php.net/manual/en/function.array-chunk.php –

回答

0
  1. 更最好使用一些PHP适配器像EloquentDoctrine,而不是运行原始查询。
  2. 使用order by

    SELECT * FROM例如为了通过字幕

  3. while循环,请使用以下的伎俩。

```

$temp = null; 

$result = []; 
while(odbc_fetch_row($biaya)) { 
    $no++; 
    $sub_title=odbc_result($biaya,"subtitle"); 
    $title=odbc_result($biaya,"title"); 
    if($temp != $sub_title) { 
     $result[$sub_title] = ["no" => $no, "subtitle" => $sub_title, "title" => $title]; 
     $temp = $sub_title; 


    } 
    else { 
     $result[$sub_title][] = ["no" => $no, "subtitle" => $sub_title, "title" => $title]; 
    } 
} 

```

+0

稍后会有许多字幕。对不起,我只是给例2副标题 –

+0

@satewedos这将工作,即使你有超过2个字幕。 – adeltahir

+0

oke。我会尝试应用它。谢谢 –

2

你可以像下面: -

$result = []; 
foreach ($array as $row){ 
    $result[$row['subtitle']][] = $row; 
} 
+0

这不会创建两个不同的变量。如OP询问 –

0

这里是解决方案..你可以看到结果here(update)

我有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); 
?> 
+0

我的数组源是(while)而不是$ array。对不起,如果我写一个模棱两可的问题 –

0
更新
<?php 
$items= 
array(
    array('no'=>"1", 'subtitle'=>"Perbekalan", 'title'=>"lombok ijo"), 
    array('no'=>"3",'subtitle'=>"Inventaris", 'title'=>"Wajan"), 
    array('no'=>"2",'subtitle'=>"Perbekalan", 'title'=>"bawang abang") 
); 

foreach($items as $item) 
    $output[$item['subtitle']][] = $item; 

extract($output); 

var_export($Perbekalan); 
echo "\n"; 
var_export($Inventaris); 

输出:

array (
    0 => 
    array (
     'no' => '1', 
     'subtitle' => 'Perbekalan', 
     'title' => 'lombok ijo', 
    ), 
    1 => 
    array (
     'no' => '2', 
     'subtitle' => 'Perbekalan', 
     'title' => 'bawang abang', 
    ), 
) 
    array (
    0 => 
    array (
     'no' => '3', 
     'subtitle' => 'Inventaris', 
     'title' => 'Wajan', 
    ), 
)