简体   繁体   English

PHP数组比较和过滤

[英]PHP Array comparison and filtering

I'm newer to PHP and sorting out some code. 我刚接触PHP并整理了一些代码。 This is taking two phone number lists… then pulling the numbers in the 2nd list OUT of the first list, making a new filtered list. 这将获取两个电话号码列表……然后将第二个列表中的数字拉出第一个列表,从而创建一个新的过滤列表。 The full code worked fine when just pulling in one list. 只需输入一个列表,完整的代码就可以正常工作。 Now that I've modified it to filter the list based a 2nd list, the code now fails and I'm getting this warning: 现在,我已经对其进行了修改以过滤基于第二个列表的列表,代码现在失败,并且我收到以下警告:

Warning: Illegal string offset 'phone_number' in /var/www/html/send.php on line 7 警告:第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'];
    }

Line 79 (where the warning comes from) is: $people[] = $row['phone_number']; 第79行(警告来自何处)是:$ people [] = $ row ['phone_number'];

Any help to pinpoint the error or an improvement on how to accomplish this filtering would be greatly appreciated! 我们将不胜感激,以帮助您查明错误或改进如何完成此过滤!

You forgot to fetch results from your resultset 您忘记从结果集中fetch结果

foreach ($result as $row) {

should be 应该

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

This can be easily done with mysql alone. 仅使用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'

your $result is a traversable object, not an array. 您的$result是可遍历的对象,而不是数组。 as seen in the docs 文档所示

Returns FALSE on failure. 失败时返回FALSE。 For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. 对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回mysqli_result对象。 For other successful queries mysqli_query() will return TRUE. 对于其他成功的查询,mysqli_query()将返回TRUE。

You can loop over the results in 2 different ways: 您可以通过2种不同的方式遍历结果:

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

Since you assign $all_people and $dnc_people with $row['phone_number'] , $filtered_people doesn't have a phone_number key, instead being number-keyed, probably. 由于您$all_people$dnc_people分配了$row['phone_number'] ,因此$filtered_people没有phone_number密钥,而可能是用数字键入的。 Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM