Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"
    }
  }
}
share|improve this question
    
What is line 29? –  adamdunson May 8 '13 at 19:05
    
@adamdunson I added more info above. –  Ricky Mason May 8 '13 at 19:16
    
paste a var_dump($data) from within your results() function –  user20232359723568423357842364 May 8 '13 at 19:19
    
@user20232359723568423357842364 added vardump for you –  Ricky Mason May 8 '13 at 19:34
    
is that a vardump from within the results() function? –  user20232359723568423357842364 May 8 '13 at 19:36
show 1 more comment

2 Answers

up vote 1 down vote accepted

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 php array_unique

share|improve this answer
add comment

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']))
share|improve this answer
    
unfortunately this did not work . I added more info above. Defining $data = array(); erased default and related. –  Ricky Mason May 8 '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 8 '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 8 '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 php.net/manual/en/language.variables.scope.php –  user20232359723568423357842364 May 8 '13 at 19:22
    
The new error is Undefined index: default and Undefined index: related its not fixed :\ –  Ricky Mason May 8 '13 at 19:27
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.