1
 array
  0 => 
    array
      'point' => string '2' 
  1 => 
    array 
      'point' => string '4' 
  2 => 
    array 
      'point' => string '1' 

I need checking values of 'point' in above array, if all values of 'point' is '4' it will return true as below array.

array
  0 => 
    array
      'point' => string '4'
  1 => 
    array
      'point' => string '4'
  2 => 
    array
      'point' => string '4'
3
  • only if whole array consist of only point =>4? Commented Jul 2, 2015 at 15:04
  • 2
    function checkArray($myArray) { $result = array_unique(array_column($myArray, 'status_id')); return (count($result) == 1 && $result[0] == '4'); } Commented Jul 2, 2015 at 15:09
  • @Toumash Yes all values point '4' Commented Jul 2, 2015 at 15:11

3 Answers 3

1

You just need to use 2 statements fomr PHP. if and for.

I used following script to test it (you can choose one of the loops(for or foreach))

$test = array(array('point' => 4), array('point' => 4));

function checkArrayForPointValue($array, $expectedValue)
{
    $ok = true;
    for ($i = 0; $i < count($array); $i++) {
        if ($array[$i]['point'] != $expectedValue) {
            $ok = false;
            break;
        }
    }
    // choose one of them

    foreach ($array as $element) {
        if ($element['point'] != $expectedValue) {
            $ok = false;
            break;
        }
    }
    return $ok;
}

print(checkArrayForPointValue($test, '4') ? 'yay' : 'not yay');
Sign up to request clarification or add additional context in comments.

Comments

0

Compare each value in a foreach :

function arrayComparePoint($array) {
$valueCompare = $array[0]['point'];
    foreach ($array as $arrayPoint) {
        foreach ($arrayPoint as $point) {
            if ($valueCompare != $point) {
                return false;
            }
        }
    }
return true;
}

Comments

0

Simply use as

function checkArray($myArray) {
    $result = array_unique(array_column($myArray, 'point'));
    return (count($result) == 1 && $result[0] == '4') ? "true" : "false";
}
echo checkArray($myArray);

Comments

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.