I have an array which gives me correct results when I print it, for example:
[0] => info#mail.com,
[1] => 0909,
[2] => info#mail.com22,
[3] => 0909
Now, when I want to check if info#mail.com is in the array it gives me an error that the value doesnt exist in this array, but when I try for example info#mail.com22 it gives the correct result.
This is a little part of the code:
$user is the word I want to search, $arrayname is the array.
if (array_search(strtolower($user),array_map('strtolower',$arrayname))){
//value exist
}
else{
//value does not exist
}
Now info#mail.com doesn't exist it says, while info#mail.com22 does exist.
Who has any idea?
array_search returns the index of the value that is found. When you search for the first item it returns 0. which is also means false. change your code so that it reads
if (false !== array_search(strtolower($user),array_map('strtolower',$arrayname))){
an alternative method would be to use in_array
if(in_array(strtolower($user),array_map('strtolower',$arrayname))){
I simply use is_numeric
if (is_numeric(array_search(strtolower($user), $arrayname)) {
/* do something */
}
Related
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.
}
}
I would like to use the first value of a dynamically created array but I get the first letter of the array name instead.
$log = "dets_".$id;
$$log = array();
while ($c = mysql_fetch_assoc($cuenta)) { array_push($$log,$c['id'].'::'.$c['fecha']); }
When I print $$log I get something like this:
Array ( [0] => 124::2017-04-07 [1] => 119::2017-04-07 [2] => 118::2017-04-05 )
But when I try to access the first key:
echo $$log[0];
I get "$d" and not "124::2017-04-07". I also tried $log[0] and get "d".
Thank you.
You could access the first element by somewhat tricky expression:
var_dump (${${'log'}}[0]);
http://php.net/manual/en/language.variables.variable.php
A bit of explanation Taken from PHP Manual
In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs
to know if you meant to use $a[1] as a variable, or if you wanted $$a
as the variable and then the [1] index from that variable. The syntax
for resolving this ambiguity is: ${$a[1]} for the first case and
${$a}[1] for the second.
$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");
I have an array that is filled with different sayings and am trying to output a random one of the sayings. My program prints out the random saying, but sometimes it prints out the variable name that is assigned to the saying instead of the actual saying and I am not sure why.
$foo=Array('saying1', 'saying2', 'saying3');
$foo['saying1'] = "Hello.";
$foo['saying2'] = "World.";
$foo['saying3'] = "Goodbye.";
echo $foo[array_rand($foo)];
So for example it will print World as it should, but other times it will print saying2. Not sure what I am doing wrong.
Drop the values at the start. Change the first line to just:
$foo = array();
What you did was put values 'saying1' and such in the array. You don't want those values in there. You can also drop the index values with:
$foo[] = 'Hello.';
$foo[] = 'World.';
That simplifies your work.
You declared your array in the wrong way on the first line.
If you want to use your array as an associative Array:
$foo=Array('saying1' => array (), 'saying2' => array(), 'saying3' => array());
Or you can go for the not associative style given by Kainaw.
Edit: Calling this on the not associative array:
echo("<pre>"); print_r($foo); echo("</pre>");
Has as output:
Array
(
[0] => saying1
[1] => saying2
[2] => saying3
[saying1] => Hello.
[saying2] => World.
[saying3] => Goodbye.
)
Building on what #Answers_Seeker has said, to get your code to work the way you expect it, you'd have to re-declare and initialise your array using one of the methods below:
$foo=array('saying1'=>'Hello.', 'saying2'=>'World.', 'saying3'=>'Goodbye.');
OR this:
$foo=array();
$foo['saying1'] = "Hello.";
$foo['saying2'] = "World.";
$foo['saying3'] = "Goodbye.";
Then, to print the contents randomly:
echo $foo[array_rand($foo)];
I am having a bit of difficulty with this. I want to allow a user to check if a username is available through an AJAX request. The AJAX request calls my php and PHP returns true if the username is not available or false if available.
I wanted to merge the username into the array (if found) and then use in_array to locate a match. It isn't working this way however.
$res = // database returns any username that matches - (not an array)
$banned = // database returns an assoc array of banned names
array_push($banned, strtolower($res['user']));
if(!in_array(strtolower($requested), $banned)){
echo 'available';
} else {
echo 'not available';
}
Here is a sample array from the banned variable:
Array
(
[0] => bad1
[1] => bad2
[3] =>
)
The 3rd key is null because it wasn't found in the $res variable.
Is there a better way to do this? I also need to convert the values in the array to lowercase as well.
For readability, I reckon this would look better
if (isset($res['user'])) { // is this key set for this array?
$banned[] = strtolower($res['user']); // append the strtolower`d version
}