I'm looking to loop through a an array and check whether the values are numbers.
for example
$rowdata is a mutltidimensional array
if (!is_int($rowData[0][0]||$rowData[0][1]||$rowData[0][2]) {
echo the value which is not a integer
}
Just for information, i already have the loop working it how i could achieve the above
EDIT:
Your if(!is_int($rowData[0][0]||$rowData[0][1]||$rowData[0][2]))
is wrong. you have to split the parameter up like this:
if(!is_int($rowData[0][0]) || is_int_($rowData[0][1]) || is_int($rowData[0][2]))
use a nested foreach-loop
$rowData = array(array(1,2,"3"),array("asdf",1,"sadf"));
foreach($rowData as $key1=>$row1){
foreach($row1 as $key2=>$row2){
if(is_int($row2)){
echo "\$rowData[$key1][$key2] is int".PHP_EOL;
}
}
}
output:
$rowData[0][0] is int
$rowData[0][1] is int
$rowData[1][1] is int
For a single array, this is a simple method to confirm that all are ints:
$a = [1, 2, 3, 'a', 4];
$allInt = array_reduce($a, function ($r, $i) { return $r && is_int($i); }, true);
So for your case:
if (!array_reduce($row[0], function ($r, $i) { return $r && is_int($i); }, true)) {
// not all are integers
}
An alternative method, which also returns you the non-integer items:
array_filter($a, function ($i) { return !is_int($i); })
So:
if ($notInt = array_filter($row[0], function ($i) { return !is_int($i); })) {
echo 'Not integers: ', join(', ', $notInt);
}