【问题标题】:Geting php error for array_merge and array_unique获取 array_merge 和 array_unique 的 php 错误
【发布时间】:2023-03-27 21:20:01
【问题描述】:

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

$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 参数包含defaultregular,如上图所示。

数据转储:

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"
    }
  }
}

【问题讨论】:

  • @adamdunson 我在上面添加了更多信息。
  • results() 函数中粘贴 var_dump($data)
  • @user20232359723568423357842364 为您添加了 vardump
  • 这是来自results() 函数的vardump吗?
  • @user20232359723568423357842364 是的,它直接来自结果()

标签: php codeigniter


【解决方案1】:

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

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

<?php
$values = array();

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

sort($values);
?>

关于 SO 的相关问题:strange behavior of php array_uniqueHow do I use array_unique on an array of arrays?

【讨论】:

    【解决方案2】:

    错误不是来自array_uniquearray_merge 函数调用。

    问题是您之前将$data 定义为string

    这应该可以解决它:

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

    【讨论】:

    • 不幸的是,这不起作用。我在上面添加了更多信息。定义 $data = array(); 擦除 defaultrelated
    • 基于 php 的错误,$data['default']$data['related'] 不存在,因为 $datastring
    • 我通过使用print_r 获得了上面的数组,所以我知道这些值存在并且是数组格式。该错误与 unique 和 merge 功能有关。在尝试您的修复时,我收到了另一个错误。我可以在不同的函数中将 $data 定义为字符串吗?
    • print_r 数组中的键是数字。您正在尝试访问字符串键。如果您遇到不同的错误,那么您的原始问题已经得到解答,您应该问另一个一。而一个函数变量只存在于函数作用域php.net/manual/en/language.variables.scope.php
    • 新的错误是Undefined index: defaultUndefined index: related 它没有修复:\
    猜你喜欢
    • 2013-03-19
    • 2011-06-07
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多