2

Below are the arrays and the code I am trying to unique/merge

$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
        )

)

It gives me an array to string error:

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: models/content_model.php

Line Number: 29

I had this problem with unique and merge before and never fixed it!

I am using codeigniter

here is more info regarding the function I am using array_unique/merge in:

    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']));
}

line 29 is the $data2['default_new'] = array_u

the $data parameter contains, default and regular which can be seen above.

vardump of data:

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"
    }
  }
}
Ricky Mason
  • 1,838
  • 6
  • 31
  • 60

2 Answers2

1

The error is not coming from the array_unique or array_merge function calls.

The problem is that you defined $data as a string previously.

This should fix it:

$data = array();
$data['default_new'] = array_unique(array_merge($data['default'], $data['related']))
  • unfortunately this did not work . I added more info above. Defining `$data = array();` erased `default` and `related`. – Ricky Mason May 08 '13 at 19:15
  • based on the error from php, `$data['default']` and `$data['related']` do not exist because `$data` is a `string` – user20232359723568423357842364 May 08 '13 at 19:17
  • I obtained the array's above by using `print_r` so I know the values exist and in array format. The error comes on the line with the unique and merge functions. I received a different error when trying your fix. Can I define $data as a string in different function? – Ricky Mason May 08 '13 at 19:20
  • the keys in your array from the print_r are numeric.. You are trying to access keys that are strings.. If you are getting a different error then your original question has been answered and you should ask another one. And a function variable only exists in the function scope http://php.net/manual/en/language.variables.scope.php – user20232359723568423357842364 May 08 '13 at 19:22
  • The new error is `Undefined index: default` and `Undefined index: related` its not fixed :\ – Ricky Mason May 08 '13 at 19:27
  • understood, but the original error is fixed.. now you are getting a new error. – user20232359723568423357842364 May 08 '13 at 19:37
1

Take a look at the Notes section here: http://us.php.net/array_unique#refsect1-function.array-unique-notes

You're going to need to come up with your own algorithm for creating a unique multidimensional array. Some of the comments at my link above suggest various methods for accomplishing this, e.g., this one:

<?php
$values = array();

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

sort($values);
?>

Related questions on SO: strange behavior of php array_unique and How do I use array_unique on an array of arrays?

Community
  • 1
  • 1
adamdunson
  • 2,635
  • 1
  • 23
  • 27