PHP array_search trouble - php

$data = Array
(
[68315163] => Donnie1
[68328887] => Donnie1
[68353339] => Donnie1
)
I want to get the all the keys for Donnie1 value it is showing only the first one
$datum = array_search('Donnie1', $data);
print_r($datum);
Where am I going wrong?

array_search() does not search array keys. It only searches array values.
Getting this value is basic PHP:
$datum = $data['68315163'];

array_search('68315163', $data) doesn't return anything useful because the value you're searching for is not in the array.
This function searches through the values, and returns the key at the found value. Please see the docs.

Array
(
[68315163] => Donnie
[68328887] => Donnie1
[68353339] => Donnie2
)
$datum = array_search('Donnie1', $data);
echo $datum;// return only value of given key: 68328887
You are passing wrong parameters to array_search(). you need to pass value of array then this function will return matching key;

array_search() does not return array. It only returns the first key.
array_keys() would be the correct function for this use. It returns an array of all keys with the given value.
$datum = array_keys($data, "Donnie1");

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

Could not search array value

I would like to search value of order using array_search function i have tried following ways but it does not work.
printed array display output
PostDaataArray
(
[order_id] => 5464
)
$currentKey=array_search($orderId,$postedData);
also tried $currentKey=array_search($orderId,array_column($postedData, 'order_id'));
But when i tried to search array using array_search function it does not work also not showing error as well.
If previously data in JSON format then decode it:
$postedData = json_decode($postedData,true);
You can use in_array():
if(in_array($orderId,array_column($postedData, 'order_id'))) //check value is in array
{
$key = array_search($orderId,array_column($postedData, 'order_id')); //return index or key of array
}
else
{
//order id not in array
}
try this:
$PostDaataArray=array("order_id"=>"5464");
foreach($PostDaataArray as $key=>$order_id){
if($order_id=="5464"){
// match found.
}
}

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

PHP get key value from array

When I put print_r($data); I get the following
Array
(
[name] => Cheese
)
Is there a way to get the key name in a variable on its own?
There may be occasions that name could be email and other values.
Use array_keys():
var_dump(array_keys($data));
Return all the keys or a subset of the keys of an array
Do you mean you know the value but you don't know the key? If so you could write something like this:
$array = ['name' => 'Cheese'];
array_flip($array);
var_export($array['Cheese']); // Output: name
You can have the array key extracted to their own variables using the extract function. For example
$a = array("color"=>"blue");
extract($a);
echo $color;

In an array match a value with key and return it's value of array

I want to search a value in and array and match with key if key exits returns it's value like if value 'foo' search in an array $array and if 'foo' found in an array returns its' value 1.
$array = array('foo'=>1, 'foo2'=>2, 'foo3'=>3);
I try with an array_key_exists() function but we know it return true or false value but I have need its value.
This should work for you:
(Here i just simply flip the array with array_flip() to search the value in the keys with in_array())
<?php
$array = array('foo'=>1, 'foo2'=>2, 'foo3'=>3);
if(in_array("foo", array_flip($array)))
echo $array["foo"];
?>

Categories