Reputation: 644
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'
Upvotes: 1
Views: 1037
Reputation: 21437
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);
Upvotes: 0
Reputation: 136
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;
}
Upvotes: 0
Reputation: 1087
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');
Upvotes: 1