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.

I get array to string conversion error for the following line:

$diff = array_diff($stockist, $arr);

Here, $arr is an array decoded from a JSON file. Using the is_array() function I was able to verify that both parameters are arrays. Can someone point me the problem

    $stockist = array();
   while (!feof($file_handle) ) 
   {
    $line_of_text = fgetcsv($file_handle);

    $query = "select * from reorderchart where medicine='".trim($line_of_text[3])."' ORDER BY medicine";

    $result = mysql_query($query);


      if(   trim($line_of_text[2])  -  trim($line_of_text[1]) <= 0 )
      {
      while($row = mysql_fetch_array($result))
         {   

            $file = "results.json";

            $arr = json_decode(file_get_contents($file),true);
            $pharmacy = trim($row['Medicine']);

            if(isset($stockist[$pharmacy]))
             {
              $medicine = $stockist[$pharmacy];
              $medicine[] = trim($row['Stockist']);
              $stockist[$pharmacy] = $medicine;
             }
            else
            {
             $medicine = array();
             $medicine[] = trim($row['Stockist']);
             $stockist[$pharmacy] = $medicine;

            }



         }

}

     }
     $diff = array();
     $diff = array_diff_assoc($stockist,$arr);
     ksort($diff);
        foreach ($diff as $key => $value) 
        {
        echo "<table align='center' border='1'>";
        echo "<tr><td align = 'center'> <font color = 'blue'> $key</td></tr>";
            foreach($value as $key1 => $value1)
              {
              echo "<tr><td align ='center'>$value1</td></tr><br>";
              }
        echo "</table>";    
        }                            
share|improve this question
2  
Show us your code –  TiMESPLiNTER Nov 7 '13 at 7:57
1  
Possible reason: multidimensional arrays –  viakondratiuk Nov 7 '13 at 8:02
    
"both parameters are arrays" Sure they are, and php says nothing different. The issue is an array to string conversion, not the other way around. The question is what do these arrays contain. –  geomagas Nov 7 '13 at 8:03
    
@viakondratiuk Yes it is mutidimensional. But doesn't array_diff() evaluate only the first level –  user2963765 Nov 7 '13 at 8:09
    
@user2963765 This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);. –  viakondratiuk Nov 7 '13 at 8:10

3 Answers 3

I've got the same error and found the following bug report for php:

https://bugs.php.net/bug.php?id=60198

Some of the array_* functions that compare elements in multiple arrays do so by (string)$elem1 === (string)$elem2.

If $elem1 or $elem2 is an array, then the array to string notice is thrown.

Two examples of functions that can throw this are array_intersect() and array_diff().

If these functions are not expected to take arrays with other arrays as values, this should be mentioned on the documentation pages.

That report describes, why php throws an error on comparing a multi-dimensional array.

share|improve this answer

According to it:

php -r 'array_diff(array("a" => array("b" => 4)), array(1));'
PHP Notice:  Array to string conversion in Command line code on line 1
PHP Stack trace:
PHP   1. {main}() Command line code:0
PHP   2. array_diff() Command line code:1

One of your arrays is multidimensional.

array_diff only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);

share|improve this answer

According to PHP documentation for the function

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

For more information refer to http://php.net/manual/en/function.array-diff.php

share|improve this answer
    
I only need to compare keys. So I used array_diff_key(). But it is returning a null array. –  user2963765 Nov 7 '13 at 10:01
    
If it's returning an empty array, it means that both arrays have the same set of keys. –  max Nov 7 '13 at 15:23

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.