PHP数组比较与过滤

PHP数组比较与过滤,php,arrays,filter,Php,Arrays,Filter,我是PHP新手,正在整理一些代码。这是两个电话号码列表…然后从第一个列表中提取第二个列表中的号码,生成一个新的过滤列表。只要拉入一个列表,完整的代码就可以正常工作。现在我已经修改了它,以根据第二个列表过滤列表,代码现在失败了,我得到了以下警告: 警告:第7行/var/www/html/send.php中的非法字符串偏移量“phone_number” // Get all of the phone numbers from the List $sql = "SELECT phone_num

我是PHP新手,正在整理一些代码。这是两个电话号码列表…然后从第一个列表中提取第二个列表中的号码,生成一个新的过滤列表。只要拉入一个列表,完整的代码就可以正常工作。现在我已经修改了它,以根据第二个列表过滤列表,代码现在失败了,我得到了以下警告:

警告:第7行/var/www/html/send.php中的非法字符串偏移量“phone_number”

    // Get all of the phone numbers from the List
$sql = "SELECT phone_number FROM dial_list WHERE list_id='$list'";
$result = mysqli_query($link, $sql);
echo mysqli_error($link);
foreach ($result as $row) 
    {
      $all_people[] = $row['phone_number'];
    }

// Get phone numbers from our DNC list
$sql = "SELECT phone_number FROM dial_dnc";
$result = mysqli_query($link, $sql);
echo mysqli_error($link);

    foreach ($result as $row) 
    {
      $dnc_people[] = $row['phone_number'];
    }

// Remove DNC numbers from list
$filtered_people = array_diff($all_people, $dnc_people);
    foreach ($filtered_people as $row)
    {
      $people[] = $row['phone_number'];
    }
第79行(警告的来源)是: $people[]=$row['phone_number']


如果您能帮助您找出错误或改进如何完成此过滤,我们将不胜感激

您忘记了从您的结果集中提取结果

foreach ($result as $row) {
应该是

while ($row = mysqli_fetch_assoc($result)) {

仅使用mysql就可以轻松做到这一点

SELECT 
dl.phone_number 
FROM dial_list AS dl 
INNER JOIN dial_dnc as dnc 
ON (dl.phone_number = dnc.phone_number)
WHERE list_id='$list'

由于使用
$row['phone\u number']
分配
$all\u people
$dnc\u people
$filtered\u people
没有
phone\u number
键,而可能是数字键。试一试

foreach($filtered_people as $key => $value)
{
    $people[] = $value;
}

您的
$result
是一个可遍历的对象,而不是数组。如所见

失败时返回FALSE。为了成功地选择、显示、描述或解释查询,mysqli_query()将返回一个对象。对于其他成功的查询,mysqli_query()将返回TRUE

您可以通过两种不同的方式对结果进行循环:

// precedural style
while ($row = mysqli_fetch_assoc($result)) { ... }
// OOP style
while($row = $result->fetch_assoc()) { ... }

foreach有3次出现。。。两个看起来像你在这里贴的,第三个是问题线。当我将前两个更改为使用mysqli_fetch_assoc时,这没有什么区别。将第三个更改为:
while($row=mysqli\u fetch\u assoc($filtered\u people)){
现在会给我以下警告:警告:mysqli\u fetch\u assoc()期望参数1是mysqli_result,数组在/var/www/html/send.php行中给出81@N8Dog:然后将此答案标记为已接受,以便其他查看此问题的人可以看到这就是解决您问题的原因。:)