0

How do I fix this error?

Notice: Array to string conversion in C:\xampp\htdocs\sugarcrm\include\SugarCharts\SugarChart.php on line 798

global $app_list_strings;
    $sortby1[] = array();
    foreach ($data_set as $row) {
        $sortby1[]  = $row[$keycolname1];
    }
    $sortby1 = array_unique($sortby1); <== line 798

I'm not familiar at all when it comes to php. Hope you can help me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hocuspocus31
  • 183
  • 1
  • 10

1 Answers1

1

Try this:-

global $app_list_strings;
    $sortby1 = array(); // define an array variable. The [] syntax is used for appending data to an array not for creating an array type variable.
    foreach ($data_set as $row) {
        $sortby1[]  = $row[$keycolname1]; // assign value to array variable
    }
    $sortby1 = array_unique($sortby1); // remove duplicate values.

Note:- check yourself that $app_list_strings and $data_set will be correctly defined and have values. Thanks.

$sortby1[] = array(); adds an array as the first element of the array $sortby1, means your array starts like this:Array ( [0] => Array() ...) and since array_unique() treats all values as strings, it's trying to convert the first element, which is an array as string, which then throws this notice.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 2
    Just for clarity, the difference is the removal of the `[]` from the name of the variable `$sortby1 = array();` (in case @hocuspocus31 couldn't spot it). – Fenton Jun 15 '15 at 20:59
  • *Try this* And what did you changed it and why? Why did OP got the notice? – Rizier123 Jun 15 '15 at 21:01
  • cool, that worked! thank you! question, why did you remove the `[ ]` from the name of the variable? – hocuspocus31 Jun 15 '15 at 21:02
  • ^ See that's why you shouldn't just write "Try this", but I told you that already a few times! – Rizier123 Jun 15 '15 at 21:03
  • *because $sortby1[] is not the correct declaration type* That just doesn't explain anything! – Rizier123 Jun 15 '15 at 21:06
  • You still didn't pointed out, why OP got the notice. – Rizier123 Jun 15 '15 at 21:12
  • Now it gets even worse, because the linked question doesn't have to do anything, with the problem, which OP has. The linked question is a problem based in OOP syntax, here we don't have anything with OOP – Rizier123 Jun 15 '15 at 21:14
  • 7 comments and 5 edits, if this only would be an exception.. (But in short: `$sortby1[] = array();` adds an array as the first element of the array `$sortby1`, means OP's array starts like this: `Array ( [0] => Array() ...)` and since `array_unique()` treats all values as strings, it's trying to convert the first element, which is an array as string, which then throws this notice ) – Rizier123 Jun 15 '15 at 21:22
  • @hocuspocus31 if the solution works for you then please mark it for the help of future visitors. thanks. Also what you asked is added into the answer. thanks. – Alive to die - Anant Jun 16 '15 at 10:56
  • @anantkumarsingh yes thank you! – hocuspocus31 Jun 16 '15 at 20:25