0

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");

   }
4

2 に答える 2

1

これらの行に沿ったもの(最初に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");
    }
}
于 2013-09-11T20:14:58.853 に答える
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");
}
于 2013-09-11T20:17:10.920 に答える