foreach loop return problem - php

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;
}
}

Related

find if key is in a multidimentional array

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;
}
}

Why this function always returning false?

I have a very basic question. It's may very poor question but I just want to clear my confusion.
function check_array($user_value,$array)
{
foreach ($array as $key => $value) {
if($value==$user_value)
{
return true;
}
return false;
}
//return false;
}
Why this function always returning false.
for example If I have $numbers = array(1,2,3). If I match 2 with this array it should return me true else return me false. But why it's returning always false ?
why it's returning always false ?
Try to "execute" this function yourself acting as a computer.
If the first element of array is equal to $user_value, it will return true. If not - it will move futher down the loop and return false.
Probably, you wanted to check all the elements of array for equality. In this case you need to use this:
function check_array($user_value,$array)
{
foreach ($array as $key => $value) {
if($value==$user_value)
{
return true;
}
}
return false;
}
The return false is in the wrong place, it should be where you commented it out but not in the other place it is.
function check_array($user_value,$array)
{
foreach ($array as $key => $value) {
if($value==$user_value)
{
return true;
}
}
return false;
}
If the return is in the other place, it will return false after failing to find your value in the first array cell.
You dont need to create a function for it when php has already have it in_array
you can use it like in a single line
$result = in_array($user_value,$array)
It will return true if the value is found and false if not found.
You need not to loop through the array
You have the return false inside of the loop. That means if the first element isn't a match, it will return false. It won't even check the second element of your array
Your function will be returning false, unless it matches the first value in the array.
This is because at the moment your logic is in the wrong order, and if the first value in the array doesn't match $user_value the function returns false.
You could solve this problem by moving the return false; line of code below the loop.
Now the loop will run through all the values in the array. It will return true if it matches one, and if it has checked all the values and there is no match then it will then return false.
function check_array($user_value,$array)
{
foreach ($array as $key => $value) {
if($value==$user_value) return true;
}
return false;
}
This should clear up the logic mistake in your code. However I up-voted Veerendra's in_array() answer as that means there is no need for any loop, cutting down your code.

How to use foreach to return true/false

How can I use foreach to return true or false.
For example, this isn't working.
function checkArray($myArray) {
foreach( $myArray as $key=>$value ) {
if(!$value == $condition) {
return false;
}
else {
return true;
}
}
if ($checkArray == true) {
// do something
}
What is the correct syntax for using foreach as true/false function? If you can't do it, could you use it to change an existing variable to true/false instead?
You would return true if the element was found/condition is true. And after the loop return false - if it had been found, this statement wouldn't have been reached.
function checkArray($myArray) {
foreach($myArray as $key => $value) {
if ($value == $condition) {
return true;
}
}
return false;
}
if (checkArray($array) === true) {
// ...
}
Of course true and false are interchangeable, depending on what output you expect.
If you're just trying to find a value you can use
if (in_array($lookup, $arrayToSearch)) { }
It'll be more efficient than enumerating the whole array.

Test only evaluating first element of array

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;
}

Returning the success or failure of processing an array using foreach

I have a PHP function that has an array as a parameter, and I am using foreach to process the array. My question is, how can i return a true or false by using the condition that the function has done the process of the foreach thoroughly without finding any failure on one of the steps of the process inside the foreach?
Something like:
function do_process($array_vars){
foreach($array_vars as $array_var) {
//do the process
}
return (foreach process success) ? true : false;
}
That depends on what you mean by "thoroughly".
You can abort as soon as something goes wrong:
function foo($array) {
foreach ($array as $foo) {
if (/* something or other */) {
return false;
}
...
}
return true;
}
Or you can continue through the rest of the array but remember that something went wrong:
function foo($array) {
$success = true;
foreach ($array as $foo) {
if (/* something or other */) {
$success = false;
}
...
}
return $success;
}
You can return from a function at any point, including from within the foreach loop when you detect failure:
function do_process($array_vars){
foreach($array_vars as $array_var) {
// do the process
if (this step of the process failed)
return false;
}
// The foreach loop must have completed successfully
return true;
}
I would do this like this:
function do_process($array_vars){
$success = true;
foreach($array_vars as $array_var){
if (some condition appeared){ $success = false; }
}
return $success;
}
You can do it that way.
function process($array_items) {
foreach($array_items as $item) {
//processing to array goes here......
if($some_bottle_neck or $failure)
return false;
if($item == end($array_items))
return true;
}
}
Hopefully this is your solution.

Categories