X9DESIGN
X9DESIGN

Reputation: 783

php - based on mysql exclude key from array and echo rest

In mysql table i got entries with those keys should be exclude from the array.

How to echo all array keys without those specifed in mysql table?

<?php
    $a = array("1","2","3","4","5","6","7","8");
?>

In table i have entries with key that should be exlude from array

<?php
    $query=mysql_query("SELECT key FROM table");
    while($get=mysql_fetch_array($query)) {
        $k=$get['key'];
    }
?>

Now i need each $k exclude from $a array and echo all others array keys.

Thank you in advance.

Upvotes: 0

Views: 286

Answers (2)

Simhachalam Gulla
Simhachalam Gulla

Reputation: 998

<?php

    $a = array("1","2","3","4","5","6","7","8");

    $query=mysql_query("SELECT key FROM table");
    while($get=mysql_fetch_array($query)) {
        $k=$get['key'];
        // check $k exists in $a array or not
        if(in_array($k, $a)){
            //get array index here
            $i = array_search($k, $a);
            unset($a[$i]); 
        }
    }
    print_r($a);

?>

It will print array values which are not found in table

Upvotes: 2

Kristian Vitozev
Kristian Vitozev

Reputation: 5971

You can use PHP's in_array function to check if current key exists in array.

if( in_array($get['key'], $a) ) {
    continue; // if key exists in $a, skip current iteration
}

Upvotes: 0

Related Questions