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>";
}
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]);
Yes, the strict answer is because "One of your arrays is multidimensional."
Another useful note might be - depending on your needs of further parsing the actual differences - consider first testing your arrays with:
$diff = strcmp(json_encode($stockist), json_encode($arr));
or
$diff = strspn(json_encode($stockist) ^ json_encode($arr), "\0");
or
$diff = xdiff_string_diff(json_encode($stockist), json_encode($arr));
All these options will compare the entire array tree, not just the top level.
Since array_diff
can only deals with one dimension, you can either:
convert your multi-dimentional array into one dimension, e.g. by:
flattening a multidimensional array, e.g.:
while ($arr) {
list($key, $value) = each($arr);
is_array($value) ? $arr = $value : $out[$key] = $value;
unset($arr[$key]);
}
// Now $out is flatten version of $arr.
serializing the array, e.g.:
$results = array_diff(array_map('serialize',$a2), array_map('serialize',$a1));
use custom recursive array_diff
-like function,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With