简体   繁体   中英

Best way to get keys and its values from an array which are not existent in another array compared only by its keys

hi I wanted to have some keys and values filtered out when compared with another array. The "content" pair should be displayed at the end...Any help is greatly appreciated THX alot!

<?php
$test = array(
            ['slideid'] => 597,
            ['token'] =>'4e23fdd176372984870a9c65db7133b5',
            ['content'] =>'<p>sdg</p>',
        )
$test2 = array(
            ['slideid'] => "",
            ['token'] =>""
            )

foreach ($test not in $test2){
    print $test2
} //not working of course   

?> 

This works:

$test = array(
    'slideid' => 597,
    'token' =>'4e23fdd176372984870a9c65db7133b5',
    'content' =>'<p>sdg</p>'
);
$test2 = array(
    'slideid' => "",
    'token' =>""
);

foreach ($test as $key => $value){
    if (!array_key_exists($key, $test2)) {
        echo $value;
    }
}

$arr contains the element that is in test but not test2

$arr = array_diff(array_keys($test), array_keys($test2));
print_r($arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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