On 1/4/2018 5:45 PM, jans wrote:
> The two included filter functions work from php manual. What I want them to do is if it is even print the number. If it is odd print "odd"; I am not getting a result in the for loop?function odd($var)
> {
> // returns whether the input integer is odd
> return($var & 1);
> }
>
> function even($var)
> {
> // returns whether the input integer is even
> return(!($var & 1));
> }
>
> $arr = array(1,2,6,7,9,4,10,11,12,3,13,24);
>
> for ($i=0; $i<= sizeof($arr); $i++){
> if ($arr_filter($arr,'even')==1){
> print_r($arr=>$i);
> }elseif($arr_filter($arr,'odd')==1)
> {
> print_r('odd');
> }
> }
> thanks,
>
array_filter operates on the entire array and returns an array. It also
is a function, not a variable ($array_filter is incorrect). The correct
way to call it would be (untested):
$even_array = array_filter($array,'even');
print_r($even_array);
$odd_array = array_filter($array, 'odd');
for ($i = 0; $i < sizeof($odd_array); $i++)
print ("odd\n");
// Alternatively you could use
foreach($odd_array as $odd)
print ("odd\n");
Of course there are many other ways - but this is one.
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================