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.
Related
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!!
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))
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;
I have an array of elements in PHP called...
$completeArray
...and I'm trying to store a randomized version of this array in my session called...
$_SESSION['videoArray']
...so I'm trying something like this...
$_SESSION['videoArray'] = shuffle($completeArray);
...but when I try to echo the first element of this randomized array like this...
$videoid = $_SESSION['videoArray'];
echo $videoid[0];
...all it's returning is the 'key' of the element. How do I randomize the array and be able to echo the actual elements of the new array?
shuffle take a reference of an array and Returns TRUE on success or FALSE on failure.
You should do:
shuffle($completeArray);
$_SESSION['videoArray'] = $completeArray;
You can try somethin like this:
$_SESSION['videoArray'] = $completeArray;
shuffle($_SESSION['videoArray']);
I have a array with a key value you on it like:
$some_array['array_key'] = "some string";
Is it possible to use array_push to add more elements to the array?
Ive tried this:
array_push($some_array['array_key'],"another string");
and I have tried other obvious way, but nothing seems to work. Is it possible to add array_push into a array with key value?
Thanks for any help you can offer,
--Bryan
If you want $some_array['array_key'] to be an array of values, you have to initialize it as an array, like this:
$some_array['array_key'] = array('some string');
Only then can you use array_push() or the [] = notation:
$some_array['array_key'][] = 'another string';