2013-05-08 48 views
2

下面是陣列和我想唯一的代碼/合併葛亭PHP錯誤array_merge和array_unique

$data['default_new'] = array_unique(array_merge($data['default'], $data['related'])) 



Array 
(
    [0] => Array 
     (
      [keywords_id] => 8 
      [keyword] => Curling 
      [parent_id] => 5 
      [count] => 0 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [keywords_id] => 8 
      [keyword] => Curling 
      [parent_id] => 5 
      [count] => 0 
     ) 

    [1] => Array 
     (
      [keywords_id] => 10 
      [keyword] => Catchers 
      [parent_id] => 6 
      [count] => 0 
     ) 

    [2] => Array 
     (
      [keywords_id] => 16 
      [keyword] => CES 2013 
      [parent_id] => 3 
      [count] => 0 
     ) 

) 

它給了我一個數組的字符串錯誤:

A PHP Error was encountered 

Severity: Notice 

Message: Array to string conversion 

Filename: models/content_model.php 

Line Number: 29 

我有這個獨特和合並之前的問題,永遠不會修復它!

我使用笨

這裏是關於我使用array_unique功能的詳細信息/合併:

public function results($data, $searched) 
    { 
     $page['searched'] = $searched; 
     $page['is_active'] = $this->logic_model->is_active(); 
     $data2 = array(); 
     $data2['default_new'] = array_unique(array_merge($data['default'], 
$data['related'])); 
} 

線29是$data2['default_new'] = array_u

$data參數包含,defaultregular其上面可以看出。

vardump數據:

array(3) { 
    ["active"]=> 
    array(2) { 
    ["id"]=> 
    string(1) "5" 
    ["keyword"]=> 
    string(6) "Sports" 
    } 
    ["related"]=> 
    array(1) { 
    [0]=> 
    array(4) { 
     ["keywords_id"]=> 
     string(1) "8" 
     ["keyword"]=> 
     string(7) "Curling" 
     ["parent_id"]=> 
     string(1) "5" 
     ["count"]=> 
     string(1) "0" 
    } 
    } 
    ["default"]=> 
    array(3) { 
    [0]=> 
    array(4) { 
     ["keywords_id"]=> 
     string(1) "8" 
     ["keyword"]=> 
     string(7) "Curling" 
     ["parent_id"]=> 
     string(1) "5" 
     ["count"]=> 
     string(1) "0" 
    } 
    [1]=> 
    array(4) { 
     ["keywords_id"]=> 
     string(2) "10" 
     ["keyword"]=> 
     string(8) "Catchers" 
     ["parent_id"]=> 
     string(1) "6" 
     ["count"]=> 
     string(1) "0" 
    } 
    [2]=> 
    array(4) { 
     ["keywords_id"]=> 
     string(2) "16" 
     ["keyword"]=> 
     string(8) "CES 2013" 
     ["parent_id"]=> 
     string(1) "3" 
     ["count"]=> 
     string(1) "0" 
    } 
    } 
} 
+0

什麼線是29? – adamdunson 2013-05-08 19:05:36

+0

@adamdunson我上面添加更多信息。 – 2013-05-08 19:16:12

+0

在你的'results()'函數中粘貼'var_dump($ data)' – user20232359723568423357842364 2013-05-08 19:19:28

回答

1

看看筆記部分在這裏:http://us.php.net/array_unique#refsect1-function.array-unique-notes

你將需要拿出自己的算法用於創建一個獨特的多維數組。在SO

<?php 
$values = array(); 

foreach($data as $d) { 
    $values[md5(serialize($d))] = $d; 
} 

sort($values); 
?> 

相關的問題:一些在我的鏈接的評論上述建議爲實現這一點的各種方法,例如,this onestrange behavior of php array_uniqueHow do I use array_unique on an array of arrays?

1

該錯誤不從array_uniquearray_merge函數調用來。

的問題是,你定義$datastring之前。

這應該修復它:

$data = array(); 
$data['default_new'] = array_unique(array_merge($data['default'], $data['related'])) 
+0

不幸的是這沒有奏效。我在上面添加了更多信息。定義'$數據=陣列();''擦除和default''related'。 – 2013-05-08 19:15:50

+0

基於php的錯誤,'$ data ['default']'和'$ data ['related']'不存在,因爲'$ data'是一個'string' – user20232359723568423357842364 2013-05-08 19:17:30

+0

我使用'print_r '所以我知道這些值是以數組格式存在的。這個錯誤來自於具有獨特的線路和合並功能。嘗試修復時我收到了一個不同的錯誤。我可以將$ data定義爲不同函數中的字符串嗎? – 2013-05-08 19:20:46