i am creating a multidimentional array and i wish to know why the following code is not working and why the other with same value when im fetching the same array is actually working.
$fruits = array(
"sale"=> array("banana", "apple", "orange"),
"regular"=> array("grapes", "pear", "ananas")
);
then in the first case it return false
1st case :
$find_price = 'sale';
if(in_array($find_price, $fruits)){
return true;
}
else {
return false;
}
and in second example i got a result of true
$find_price = 'sale';
if(isset($fruit[$find_price])){
return true;
}
else {
return false;
}
in_array() used to determine the value is in array or not. If you want to find if the key exist so array_key_exists is your friend
Look at the below snippet.
$find_price = 'sale';
if(array_key_exists($find_price, $fruits)){
return true;
}
else {
return false;
}
In your first code
$find_price = 'sale';
if(in_array($find_price, $fruits)){
return true;
}
else {
return false;
}
You use in_array(). This in_array() function the elements into the array, That element exist in array or not. And you are finding a value which is key in the array. Instead of in_array() you can use array_key_exists().
Your second code
$find_price = 'sale';
if(isset($fruit[$find_price])){
return true;
}
else {
return false;
}
You are using isset() this function tell that the element you find, is exist in code or not. Like you are finding isset($fruit[$find_price]) means isset($fruit['sale']) that is exist....
Thats why this condition is true..
You have to use loop for this type of conditions. try this.
foreach($fruits as $key => $value)
{
if($fruits[$key]['sale'])
{
return true;
}
else
{
return false;
}
}
Related
I have an array like this:
$array = array('static_value_1', 'static_value_2', 'extra_value_1', 'extra_value_2');
How could I return true if any value exists in array in addition to static_value_1 and/or static_value_2?
For example this array should return true:
array = array('static_value_1', 'static_value_2', 'extra_value_1');
and this one false:
array = array('static_value_1', 'static_value_2');
Thanks!
I think just looping looking for non-static should work:
function check_array($check) {
$static=Array('static_value_1', 'static_value_2');
// Dealing with empty array.
if(count($check)==0) return false;
foreach($check as $value) {
// If value is not in static collection is an addition...
if(!in_array($value, $static)) {
return false;
}
}
return true;
}
I'm looking to see if an array has one or more values inside it and I want to pass check my values are in that array. I'm going to use it to check role permissions in a system.
example input-output:
[1,2,3,4,5,6].include?([4,1]) => true
[4,1,6,2].include?([4,1]) => true
[3,4,7].include?([4,1]) => false
I tried to solve it using in_array():
if(!$auth->checkPermissions([1,3])) {
echo json_encode(['Result'=>false,'Type'=>'ERROR','Message'=>UNAUTHORIZED_ACCESS]);
exit;
}
public static function checkPermissions($permissionLevels = []){
$userPermission =[1,4,5,6];
foreach($permissionLevels as $permission){
if (in_array($permission,$userPermission)) {
return true;
} else {
return false;
}
}
return false;
}
Your problem is happening because you only checking 1 permission as you use return in the foreach.
You should do it like this:
public static function checkPermissions($permissionLevels = []){
$userPermission =[1,4,5,6];
foreach($permissionLevels as $permission){
if (!in_array($permission,$userPermission))
return false; // if one is missing decline
}
return true; // if got here mean all found
}
You can also use array-intersect as:
function checkPermissions($permissionLevels = []){
$userPermission =[1,4,5,6];
$permissionLevels = array_unique($permissionLevels);
return count(array_intersect($permissionLevels, $userPermission)) == count($permissionLevels));
}
Do notice to use array_unique if using the array_intersect as you can have duplicate - consider case where checkPermissions([4,4]);
Just compare the intersection of the two with the permissions to be checked:
return array_intersect($permissionLevels, $userPermission) == $permissionLevels;
See the Demo.
I loop through the values of a form, to check that each field has 4 digits. My problem is currently it validates true or false only on the match for the first field $card1...
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
else
{
return true;
}
}
You're returning (by using return ...) something in the first iteration every time (boolean condition with an else).
You need to put the return true outside the loop statement:
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++) {
if (! preg_match ($regex,$cards[$i])) {
return false;
}
}
return true;
}
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
return true;
}
I have an $array on php, and I'd like to know if the values are from a specific dictionary.
For example, if my dictionary is an array of values ['cat', 'dog', 'car', 'man'] I'd like to filter my $array and return false if a word into this one is not on the dictionary.
So, if $array is :
['men', 'cat'] // return false;
['man', 'cat'] // return true;
['cat', 'dogs'] // return false;
[''] // return false;
and so on...
How can I filter an array in this manner?
function checkDictionary($array,$dictionary){
foreach($array as $array_item){
if(!in_array($array_item,$dictionary)){
return false;
}
}
return true;
}
you can also do something like:
function checkDictionary($array,$dictionary){
$result = (empty(array_diff($array,$dictionary))) ? true : false;
return $result;
}
To increase performance, you should convert you "dictionary" to an associative array, where the keys are the words in the dictionary:
$dict = array_flip($dict);
Then all you have to do is to loop over the values you search for, which is O(n):
function contains($search, $dict) {
foreach($search as $word) {
if(!array_key_exists($word, $dict)) {
return false;
}
return true;
}
Reference: array_flip, array_key_exists
function doValuesExist($dictionary, $array) {
return (count(array_intersect($dictionary,$array)) == count($array));
}
hi i write a function to find in array but its not working when loop find something match its not retuning true value checks to the end any idea
function findinArray($find,$array){
foreach($find as $key => $value){
if (in_array($find,$array)) {
return true;
}else{
return false;
} }
}
if(findinArray(array("a","b"),array("a")){
echo "Match";
}
thanks
A function can only return once, so your function will always return on the first iteration. If you want it to return true on the first match, and false if no match was found, try the version below.
function findinArray($find, $array) {
foreach ($find as $value) {
if (in_array($value, $array)) {
return true;
}
}
return false;
}
if (findinArray(array("a","b"), array("a")) {
echo "Match";
}
(You had also made errors in how you use the values in the foreach, and you have forgotten a })
It should be in_array($value, $array). But you could just do count(array_intersect()).
you are passing first argument an array in in_array() it should be the value
change it to
function findinArray($find,$array){
foreach($find as $key => $value){
if (in_array($value,$array)) {
return true;
}
return false;
}
}