0

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).

1
  • 1
    If you need to check against the previous value, why are you then checking against the array key? - I'd strongly suggest you read up on some tutorials with basic PHP / arrays. Commented Oct 11, 2016 at 11:18

7 Answers 7

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I do that sort of stuff as well ;-/ it made me smile :)
That's what I'm here for, Ryan, that's all I'm here for… :o)
0

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

3 Comments

when I create a function with that inside it seems to work, thank you. But I'm trying to figure out what its going on with your code. The way I see it. you put all values of $array into the first key of $temp? And check if $key is smaller then $temp and if so it returns "false" or else true?
I put first value of $array in $temp (temp is not array, it's just variable which holds one value at the time). So I put one first value in temp and check if it is smaller (we can then set value as false and stop checking other values), if not, I assign value to temp and check if next time value which will give me foreach is smaller etc. If everytime it was greater, we never assign false, so our val stays true
It put first value and compare with first (just for cleaner code, we don't loose almost any performance power), then first with second, then second with third etc.
0
$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

Comments

0

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

?>

Comments

0

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

Comments

-1

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

}

4 Comments

$key[0] will always refer to the same key
It stil returns true 4x. I think because all the values are bigger then the first key.
If anything you want $value >= $input[$key - 1]
I do some changes I think that the code is working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.