2013-02-17 38 views
0

我在數據庫表中有3行是json數據,我希望它們合併它們在一個數組中,如何解決它?合併3行具有json值

第1列: [ 「11,22,13」]
第2列: [ 「48」]
第3列: [ 「53,67,70」]

我想輸出:array(11,22,13,48,53,67,70)

我試圖爲:

$result = $this->db->get_where('table',array('mainpage'=>$mp')); 
    $data = array(); 
    foreach($result->result() as $row){ 
     $dv = json_decode($row->sbouy); 
     $out = array(); 
     foreach($dv as $idx => $val){ 
      $out[] = $val; 
     } 
     echo '<pre>'; 
     print_r($out); // This is not what I want output 
    } 

在我的代碼輸出結果(這不是我所希望輸出):

Array 
(
    [0] => 11,22,13 
) 
Array 
(
    [0] => 48 
) 
Array 
(
    [0] => 53,67,70 
) 

回答

0

使用此

$data = array(); 
    $out = array(); 
    foreach($result->result() as $row){ 
     $dv = json_decode($row->sbouy); 

     foreach($dv as $idx => $val){ 
      $out[] = $val; 
     } 

    } 
    echo '<pre>'; 
    print_r($out); // This is what you want :) 

你必須定義$out外的foreach。而不是循環你甚至可以使用array_merge

+0

如何使用array_merge,請說明一個例子?我想輸出爲:array(11,22,13,48,53,67,70) – 2013-02-17 07:50:23

+0

$ out = array_merge($ dv,$ out);使用這個代替循環 – Venu 2013-02-17 07:51:46

+0

但我的輸出爲:'Array ( [0] => 53,67,70 [1] => 48 [2] => 11,22,13 );'我的php代碼如下:'foreach($ result-> result )爲$ row){ \t \t $ dv = json_decode($ row-> subpage); \t \t $ out = array_merge($ dv,$ out); \t \t}'我想輸出爲:數組(11,22,13,48,53,67,70)如何解決它? – 2013-02-17 09:19:12