jbenning
jbenning

Reputation: 21

PHP - Check if current array number is >= then the last one

Hello I'm new here on this forum.

I was wondering if someone could help me out here. I'm following an IT study currently and one of the subjects I have trouble with is PHP programming. I currently have problem with the following question.

Question : Create a function that checks the values(numbers) in an array. If the current value is bigger than or equals to the one before that, it should echo true. If not, echo false.

True example: $array = array (1,2,2,3,4) this should say true 4 times because it gets bigger & the number stays the same.

False Example: $array = array (1,2,1,4) this should say false because the number is going down.

I have made the following code and it will always return "true".

$input = array (1,4,2,7);

function trueorfalse (array $input){

    foreach ($input as $key => $value){
        $value >= $key[0];
        print "true";
    }
 }

trueorfalse($input);

I have not idea how to compare the current value the foreach loop is at, with the value before that. Any ideas?

PS: PHP is new for me and I really have trouble figuring out how to come up with ideas on how to solve a question.

Yours truly,

Joey Benning IT student Windesheim Zwolle (Netherlands).

Upvotes: 0

Views: 1016

Answers (7)

Antoine Delia
Antoine Delia

Reputation: 1845

This function checks all the values in your array beginning with the second value.

for($i = 1; $i < sizeof($input); $i++) 
{
    //We check if the value is greater or equal than the one before
    if($input[$i] >= $input [$i-1])
        //If it is true, we'll display it
        echo "True";
    //Else, we display false
    else
        echo "False";
}

Upvotes: 0

Rodrigo Brand&#227;o
Rodrigo Brand&#227;o

Reputation: 19

You just forgotten the if:

$input = array (1,4,2,7);
function trueorfalse (array $input)
{
   foreach ($input as $key => $value){
       if ($value >= $key[0])
           print "true";
   }
 }

trueorfalse($input);

EDIT: Just in time I switch to for loop:

function trueorfalse (array $input)
{
    for($i = 0; $i < sizeof($input)-1;$i++)
    {
        echo "$input[$i] >= {$input[$i+1]} => ";
        echo ($input[$i] >= $input[$i+1]) ? "true <br>" : "false <br>";
    }

}

Upvotes: -1

deceze
deceze

Reputation: 522175

Not the most efficient or comprehensible way, but yay for array reduction:

$result = array_reduce($input, function (array $carry, $num) {
    return [$carry[0] && $num >= $carry[1], $num];
}, [true, PHP_INT_MIN])[0];

echo $result ? 'true' : 'false';

Alternative:

$result = is_int(array_reduce($input, function ($previous, $current) {
    return is_int($previous) && $current >= $previous;
}, PHP_INT_MIN));

echo $result ? 'true' : 'false';

Upvotes: 1

arkascha
arkascha

Reputation: 42925

This would be my straight forward approach:

<?php
function isHigherOrEquals (array $input) {
    $result = true;
    $previous = PHP_INT_MIN;
    foreach ($input as $value) {
        if (!($value >= $previous)) {
            return false;
        }
        $previous = $value;
    }
    return $result;
}
var_dump(isHigherOrEquals([1,4,2,7]));
var_dump(isHigherOrEquals([1,4,6,7]));
var_dump(isHigherOrEquals([1,1,1]));
var_dump(isHigherOrEquals([]));

The output obviously is:

bool(false)
bool(true)
bool(true)
bool(true)

Upvotes: 1

Jwags
Jwags

Reputation: 502

Here you go.

I used a for loop. It is a biot easier when comparing multiple elements in an array. If you are new/ inexperienced with coding, I suggest PHP. PHP was one of the first languages I learned and it was not too difficult. Once you get familiar with coding, I would move to an object oriented language, like C, C++, Java. PHP is good, but it is a scripting language and it is good to have experience with both types of programming languages.

Hope this helps.

<?php
function CheckArray($arrayToCheck) {
    for ($i = 1; $i < count($arrayToCheck); $i++) {
        echo(($arrayToCheck[$i]>=$arrayToCheck[$i-1]? $arrayToCheck[$i].">=".$arrayToCheck[$i-1].": True<br />" : $arrayToCheck[$i].">=".$arrayToCheck[$i-1].": False<br />"));
    }
}

$array = array(1, 2, 3, 4, 5, 4, 3, 2, 1);
CheckArray($array);

?>

Upvotes: 0

andreini
andreini

Reputation: 198

$array = array (1,2,1,4);
$lastChecked = 0;//we need to store the last checked value

foreach($array as $key => $value)
{
    //compare the current array value with the last stored one
    if($value >= $valastCheckedue)
    {
        echo "True<br>";
    }else{
             echo "False<br>";
         }

    //store the current checked value from the array
    $lastChecked = $value;

}

Will output:

True
True
False
True

Upvotes: 0

Alan Mroczek
Alan Mroczek

Reputation: 1199

You can set temp variable

<?php

$array = array(1, 2, 2, 3, 4);

$temp = $array[0];

$val = true;

foreach ($array as $key) {

    if ($key < $temp) {
        $val = false;
        break;
    } else {
        $temp = $key;
    }

}

var_dump($val);

You can replace true and false with strings "true" and "false", and then echo it instead of var_dump

Upvotes: 0

Related Questions