¿Cómo buscar en la matriz php todos los elementos de la tabla mysql?

I want to display only usernames that doesn't exist in usernameArray and only exist in mysql table. in another word for each username in mysql table i want to check it inside usernamesArray and if doesn't exist in that array i just output that username from mysqltable? could anyone show me how i can achieve this task.Thanks

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

   }

preguntado el 11 de septiembre de 13 a las 21:09

To avoid confusion, please remove the unnecessary code and include only the parte. -

2 Respuestas

Intente cambiar:

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

a:

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

Respondido el 11 de Septiembre de 13 a las 21:09

thanks . is there away to count $record responses without using counter ? - user1788736

There certainly is: $record_count = count($record); - Buchow_PHP

Something along these lines (you need to store the SQL rows first, then check 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");
    }
}

Respondido el 11 de Septiembre de 13 a las 21:09

thanks for reply. i already stored usernames in array from api respone cant i just loop the mysql table and check inside that loop ? instead of saving both api response and mysql table data into arrray ? - user1788736

Yes, sorry, I reversed things. You generally need to use in_array like the above poster mentioned. - tim b

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.