2013-05-08 45 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