【问题标题】:How to search php array for all items from mysql table?如何在 php 数组中搜索 mysql 表中的所有项目?
【发布时间】:2013-09-11 20:06:04
【问题描述】:

我只想显示 usernameArray 中不存在且仅存在于 mysql 表中的用户名。换句话说,对于 mysql 表中的每个用户名,我想在 usernamesArray 中检查它,如果该数组中不存在,我只是从 mysqltable 输出该用户名?谁能告诉我如何完成这项任务。谢谢

       $url = "https://api.instagram.com/v1/users/XXXX/follows?access_token=XXXX&count=-1";
       $api_response = get_data(''.$url);
       $record = json_decode($api_response); // JSON decode


$m = 0;

$usernamesArray = array();

foreach($record->data as $user) // each user data (JSON array) defined as $user
{
 $m++;

$usernameVar = $user->username;
$usernamesArray[] = $usernameVar;

}

print_r($usernamesArray);


$sql->Query("SELECT * FROM mytable");

echo "Total:".$sql->rows;
echo "<br>";
    for ($i = 0; $i < $sql->rows; $i++) 
    {
        $sql->Fetch($i);
        $id = $sql->data[0];
        $username = $sql->data[1];
        $website = $sql->data[2];
        $profile_picture = $sql->data[3];


//now compare usernamesArray with current data in mysql table and only display 
//the usernames that doesnt exist in usernamesArray?

 echo("<div id='grid-cell' style='padding:5px'><a style='text-decoration:none' href='$username'><img class='photo-grid' src='$profile_picture' width=150 height=150 title='$username' /></a><div class='moreInfo2'><a style='color:#000;text-decoration:none' href='/$username' target='_blank'>$item:$username()</a></div></div>\n");

   }

【问题讨论】:

  • 为避免混淆,请删除不必要的代码并仅包含相关部分。

标签: php mysql arrays instagram


【解决方案1】:

尝试改变:

echo("<div id='grid-cell' style='padding:5px'><a style='text-decoration:none' href='$username'><img class='photo-grid' src='$profile_picture' width=150 height=150 title='$username' /></a><div class='moreInfo2'><a style='color:#000;text-decoration:none' href='/$username' target='_blank'>$item:$username()</a></div></div>\n");

到:

if(!in_array($username, $usernamesArray))
{
echo("<div id='grid-cell' style='padding:5px'><a style='text-decoration:none' href='$username'><img class='photo-grid' src='$profile_picture' width=150 height=150 title='$username' /></a><div class='moreInfo2'><a style='color:#000;text-decoration:none' href='/$username' target='_blank'>$item:$username()</a></div></div>\n");
}

【讨论】:

  • 谢谢。是否可以在不使用计数器的情况下计算 $record 响应?
  • 肯定有:$record_count = count($record);
【解决方案2】:

类似这样的事情(您需要先存储 SQL 行,然后检查 in_array):

$usernamesArray = array();

$sql->Query("SELECT * FROM mytable");
for ($i = 0; $i < $sql->rows; $i++) {
    $sql->Fetch($i);
    $usernamesArray[] = $sql->data[1];
}

foreach ($record->data as $user) {
    $usernameVar = $user->username;
    if (!in_array($usernameVar,$usernamesArray)) {
        echo("<div id='grid-cell' style='padding:5px'><a style='text-decoration:none' href='$usernameVar'><img class='photo-grid' src='$profile_picture' width=150 height=150 title='$username' /></a><div class='moreInfo2'><a style='color:#000;text-decoration:none' href='/$usernameVar' target='_blank'>$item:$usernameVar()</a></div></div>\n");
    }
}

【讨论】:

  • 感谢您的回复。我已经将用户名存储在来自 api 响应的数组中,我不能只循环 mysql 表并检查该循环内部吗?而不是将 api 响应和 mysql 表数据都保存到数组中?
  • 是的,对不起,我把事情弄反了。您通常需要像上面提到的海报一样使用 in_array。
猜你喜欢
  • 1970-01-01
  • 2021-01-21
  • 2020-04-27
  • 2020-12-26
  • 2016-08-31
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
相关资源
最近更新 更多