获取 array_merge 和 array_unique 的 php 错误

问题描述 投票:0回答:3

下面是我尝试唯一/合并的数组和代码

$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

我之前遇到过 unique 和 merge 的问题,但从未修复过!

我正在使用codeigniter

这里是有关我正在使用 array_unique/merge 的函数的更多信息:

    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
参数包含
default
regular
,如上所示。

数据的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"
    }
  }
}
php codeigniter
3个回答
1
投票

查看此处的注释部分:http://us.php.net/array_unique#refsect1-function.array-unique-notes

您将需要提出自己的算法来创建唯一的多维数组。我上面链接中的一些评论建议了实现此目的的各种方法,例如,this one

<?php
$values = array();

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

sort($values);
?>

SO 相关问题:php array_unique 的奇怪行为如何在数组数组上使用 array_unique?


1
投票

错误不是来自

array_unique
array_merge
函数调用。

问题是您之前将

$data
定义为
string

这应该可以解决它:

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

0
投票

公共静态函数appClasses() {

$data = config('custom.custom');


// default data array
$DefaultData = [
  'myLayout' => 'vertical',
  'myTheme' => 'theme-default',
  'myStyle' => 'light',
  'myRTLSupport' => true,
  'myRTLMode' => true,
  'hasCustomizer' => true,
  'showDropdownOnHover' => true,
  'displayCustomizer' => true,
  'contentLayout' => 'compact',
  'headerType' => 'fixed',
  'navbarType' => 'fixed',
  'menuFixed' => true,
  'menuCollapsed' => false,
  'footerFixed' => false,
  'customizerControls' => ['rtl','style','headerType','contentLayout','layoutCollapsed','showDropdownOnHover','layoutNavbarOptions', 'themes', ],
  //   'defaultLanguage'=>'en',
];

$data= array_unique(array_merge($DefaultData,$data));

© www.soinside.com 2019 - 2024. All rights reserved.