为什么和array_diff()给数组字符串转换错误? [英] Why array_diff() gives Array to string conversion error?
本文介绍了为什么和array_diff()给数组字符串转换错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到数组字符串转换错误以下行:
$ =差异和array_diff($配货,$ ARR);
在这里, $改编
是从JSON文件数组德codeD。使用 is_array()
功能我可以确认这两个参数是数组。有人能指出我这个问题。
$配货=阵列();
而(!的feof($ file_handle))
{
$ line_of_text = fgetcsv($ file_handle); $查询=从reorderchart选择*其中药='修剪($ line_of_text [3])。'ORDER BY药; $结果= mysql_query($查询);
如果(修剪($ line_of_text [2]) - 修剪($ line_of_text [1])&下; = 0)
{
而($行= mysql_fetch_array($结果))
{ $文件=results.json; $ ARR = json_de code(的file_get_contents($文件),TRUE);
$药房=修剪($行['医学']); 如果(使用isset($配货[$药房))
{
$医学= $配货[$药店]
$医药[] =修剪($行['配货']);
$配货[$药店] = $药;
}
其他
{
$医学=阵列();
$医药[] =修剪($行['配货']);
$配货[$药店] = $药; } }
}
}
$差异=阵列();
$差异和array_diff_assoc =($配货,$ ARR);
ksort($差异);
的foreach($差异为$关键=> $值)
{
回声<表ALIGN =中心的边界='1'>中;
回声< TR>< TD ALIGN ='中心'><字体颜色=蓝> $钥匙< / TD>< / TR>中;
的foreach($值$键1 => $值1)
{
回声< TR>< TD ALIGN ='中心'> $值1< / TD>< / TR>< BR>中;
}
回声< /表>,
}
解决方案
据它:
PHP -r'和array_diff(阵列(A=>阵列(B=> 4)),阵列(1));'
PHP的注意:数组字符串转换在命令行code 1号线
PHP堆栈跟踪:
PHP 1. {}主()命令行code:0
PHP和array_diff 2.()命令行code:1
你的一个数组是多维的。
和array_diff
只检查了多维数组中的一维。当然,你可以通过使用检查更深的维度和array_diff($数组1 [0],$数组2 [0]);
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]);
这篇关于为什么和array_diff()给数组字符串转换错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文