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"
    }
  }
}
5
  • @adamdunson I added more info above. Commented May 8, 2013 at 19:16
  • paste a var_dump($data) from within your results() function Commented May 8, 2013 at 19:19
  • @user20232359723568423357842364 added vardump for you Commented May 8, 2013 at 19:34
  • is that a vardump from within the results() function? Commented May 8, 2013 at 19:36
  • @user20232359723568423357842364 yes its straight from results() Commented May 8, 2013 at 20:14

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

unfortunately this did not work . I added more info above. Defining $data = array(); erased default and related.
based on the error from php, $data['default'] and $data['related'] do not exist because $data is a string
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?
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
The new error is Undefined index: default and Undefined index: related its not fixed :\
|
0
public static function 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));
}

2 Comments

It gives me an error: $data= array_unique(array_merge($DefaultData,$data));
Do not post an answer because you have a similar problem -- that is not how Stack Overflow's Q&A works. See the comprehensive list of techniques that you might use to remove duplicates at: Filter/Remove rows where column value is found more than once in a multidimensional array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.