Check if value of an array is present in another array - php

I am trying to check if value of an array $spam is present in array $get_mail.
I have following code, but it doesnt seem to work or I dont understand it properly.
$spam_exists = !array_diff($spam, $get_mail);
if ($spam_exists !== FALSE) { ... }
Any idea why this doesnt work?
Thank you for any reply.

Use the array_intersect function.
$result = array_intersect($spam, $get_mail);
Which will return the values in both arrays as an array, or an empty array if there are no shared results.
So rather than using !array_diff($X,$Y) you could use !empty(array_intersect($X,$Y)) or simply if(array_intersect($X,$Y))

Related

Array as variable for in_array

I have a table that I am reading two columns from using PDO::FETCH_KEY_PAIR. I then need check if a value from another query exists in the array returned by the PDO fetch. To illustrate:
$id = array();
/* array returned by PDO::FETCH_KEY_PAIR query */
$mailTo = array (
'MailTo1' => 6143,
'MailTo2' => 6137,
'MailTo3' => 6137,
);
echo $mailTo['MailTo1']; //6143
$result1['needle'] = 'MailTo1'; //needle from second query to seek
if(in_array($result1['needle'], $mailTo)){
$id['mailTo'] = $mailTo[$result1['needle']]; //null
}
using variable $result['needle'] returns null, but to verify that in_array returns the correct element I have used:
if(in_array('MailTo1', $mailTo)){
$id['mailTo'] = $mailTo[$result['needle']]; //6143
}
Hard coding the needle returns the correct element. Taking the needle out an array and passing as a simple variable also returns null, i.e.
$result = 'MailTo1';
if(in_array($result1, $mailTo)){
$id['mailTo'] = $mailTo[$result1]; //null
}
I cannot figure out why passing the needle as a key=>value variable ($result1['needle']) fails. It seems like this is very common practice...
In order to compare keys you need to use array key exists. in_array only looks at values and not the keys.
array_key_exists("MailTo1",$mailTo)
Another approach would be to get all keys in one array and then you can use in_array()
$mailToKeys = array_keys($mailTo);
in_array("MailTo1", $MailToKeys)
DOH!!! Wrong approach.. array_key_exists is what I should have been using!!

array_dif not working as the output is empty

I created 2 arrays and want to check for the difference between both arrays (values). If I use my arrays with the function array_diff, the response is a empty Array, which is very wierd as I can't find the problem at all.
My setup:
// first array
$listing_products_sku = [
'55995', '55996', '55999', '56000', '56005', '56006', '56007',
'56008', '56021', '56022', '56023', '56024', '56029', '56030',
'56031', '56032', '56036', '56037',
];
// second array:
$internal_products_sku = [
'56015', '56016', '56014', '56018', '56019', '56020', '55994',
'55995', '55996', '55997', '55998', '55999', '56000', '56001',
'56002', '56003', '56005', '56004', '56006', '56007', '56008',
'56009', '56010', '56011', '56012', '56013', '56017', '56021',
'56022', '56023', '56024', '56025', '56026', '56027', '56028',
'56029', '56030', '56031', '56032', '56033', '56034', '56035',
'56036', '56037', '56038', '56039', '56040', '56041', '60434',
'60435',
];
// used function:
$diff_result = array_diff($listing_products_sku, $internal_products_sku);
print_r($diff_result);
Output
Array ( )
Help needed
Is anybody able to explain why this happens and how I can make this working?
array_diff() returns array from first array containing values not exist in rest of the arrays (http://php.net/manual/en/function.array-diff.php). As your first array's element are already exist in second array ($internal_products_sku) this is why its returning empty array.
So to find the difference all you have to do is take the $internal_products_sku array as first param then check
$diff_result = array_diff($internal_products_sku, $listing_products_sku);
print_r($diff_result);
Now it will return an array with those value not exist in $listing_products_sku

Why array_key_exists() on array of array doesn't work properly?

I've an array of array in php. Both arrays are non indexed (they use keys).
$this->confArr["$sectionName"] = Array(); // case 1
This returns true:
isset($this->confArr["$sectionName"]);
Because an element with name $sectionName was already set.
$this->confArr["$sectionName"]["$itemKey"] = $itemValue; //case 2
I cannot figure out why but this returns always FALSE
array_key_exists($itemKey, $this->confArr["$sectionName"]);
What's the problem ?
The problem was this:
array_key_exists($itemKey, $this->confArr["$sectionName"]);
Should be:
array_key_exists("$itemKey", $this->confArr["$sectionName"]);
Don't know exactly why but it works this way ?

how to check if the second array key is an array?

i have an array which could look like this:
$config[$name][$array]['key'] = 'value'; // here it's an array
or
$config[$name][$array] = 'value'; // here it's not an array
i want to check if the second array key ('array') is an array or not.
could someone help me?
use the function is_array http://php.net/manual/en/function.is-array.php
if(is_array($config[$name][$array])) echo "Yup I'm an array";
PHP has a built in function, is_array. That should let you know whether or not you have an array, or a plain string.

printing from multidimensional arrays?

I have a multi array that looks like this:
$_SESSION['cartItems']['quantity']
How do I print out its values? print_r won't work, unless $_SESSION doesn't support multi-dimensional arrays?
print_r($_SESSION['cartItems']); should work.
you can use var_dump($_SESSION). However, print_r should work. Make sure you are doing print_r($_SESSION), and not trying to print_r your variable that may not exist.
If you want to get the quantity of each card item in the array, you can do this:
$quantities = array_map(create_function('item', "$item['quanitity']"), $_SESSION['cardItems']);
// PHP 5.3
$quantities = array_map(function($item) {
return $item['quanitity'];
}, $_SESSION['cardItems']);

Categories