array_dif not working as the output is empty - php

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

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!!

Check if value of an array is present in another array

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

PHP JSON Arrays within an Array

I have a script that loops through and retrieves some specified values and adds them to a php array. I then have it return the value to this script:
//Returns the php array to loop through
$test_list= $db->DatabaseRequest($testing);
//Loops through the $test_list array and retrieves a row for each value
foreach ($test_list as $id => $test) {
$getList = $db->getTest($test['id']);
$id_export[] = $getList ;
}
print(json_encode($id_export));
This returns a JSON value of:
[[{"id":1,"amount":2,"type":"0"}], [{"id":2,"amount":25,"type":"0"}]]
This is causing problems when I try to parse the data onto my android App. The result needs to be something like this:
[{"id":1,"amount":2,"type":"0"}, {"id":2,"amount":25,"type":"0"}]
I realize that the loop is adding the array into another array. My question is how can I loop through a php array and put or keep all of those values into an array and output them in the JSON format above?
of course I think $getList contains an array you database's columns,
use
$id_export[] = $getList[0]
Maybe can do some checks to verify if your $getList array is effectively 1 size
$db->getTest() seems to be returning an array of a single object, maybe more, which you are then adding to a new array. Try one of the following:
If there will only ever be one row, just get the 0 index (the simplest):
$id_export[] = $db->getTest($test['id'])[0];
Or get the current array item:
$getList = $db->getTest($test['id']);
$id_export[] = current($getList); //optionally reset()
If there may be more than one row, merge them (probably a better and safer idea regardless):
$getList = $db->getTest($test['id']);
$id_export = array_merge((array)$id_export, $getList);

PHP adressing the first key in an array that changes

For example, lets say I have an array that looks liked
$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');
and then I call the asort() function and it changes the array around. How do I get the new first string without knowing what it actually is?
If you want to get the first value of the array you can use reset
reset($stuff);
If you want to also get the key use key
key($stuff);
If you need to get the first value, do it like this:
$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');
$values = array_values($stuff); // this is a consequential array with values in the order of the original array
var_dump($values[0]); // get first value..
var_dump($values[1]); // get second value..

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 ?

Categories