Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: filter function for odd and even in a for loop

19 views
Skip to first unread message

Jerry Stuckle

unread,
Jan 4, 2018, 8:01:06 PM1/4/18
to
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
==================

Arno Welzel

unread,
Jan 4, 2018, 8:07:48 PM1/4/18
to
jans:

> 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?[...]
> 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');
> }
> }

What is this? This code makes no sense at all.

Turn on error reporting and see for yourself.


--
Arno Welzel
https://arnowelzel.de
https://de-rec-fahrrad.de
http://fahrradzukunft.de

Mark Lloyd

unread,
Jan 5, 2018, 3:11:44 PM1/5/18
to
On 01/04/2018 04: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,
>

The biggest problem here may be using an uninitialized variable
($arr_filter) instead of a function name. Perhaps you meant array_filter?

This function returns an array. Having a loop here doesn't make sense
(since print_r already does it).

How about replacing the loop with:

echo "Odd :\n";
print_r(array_filter($arr, 'odd'));
echo "Even:\n";
print_r(array_filter($arr, 'even'));

--
Mark Lloyd
http://notstupid.us/

"I believe Christ was a man like ourselves; to look upon him as God
would seem to me the greatest of sacrileges" [Leo Tolstoy]

J.O. Aho

unread,
Jan 5, 2018, 3:52:13 PM1/5/18
to
On 01/04/18 23:45, 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

You do only need one of the functions and you do not apply it as a
filter, but evaluate each value and then decide if you want to echo the
value itself or the word odd

function even($var) {
// returns true if the input integer is even
return(!($var & 1));
}

$arr = array(1,2,6,7,9,4,10,11,12,3,13,24);

foreach($arr as $val) {
echo (even($val) ? $val : "odd") . "\n";
}


filtering is a useful tool if you want to exclude something from your
array, as you don't (you are not just showing which values are even, you
are also telling if there is an odd value among the even ones) want to
filter you have to solve this with a loop and evaluate each value and do
action based on the result.

Don't forget that filtering will make php to loop trough your array, so
filtering in this case would maker things slower.

Sure you could check things within your filter, have an echo inside and
then return the result, but that's not really how to use filtering in a
good way.

--

//Aho

inigoj...@gmail.com

unread,
Jan 23, 2018, 5:05:28 AM1/23/18
to
If we wanted to get all the even (or odd) numbers, we could use the above function “even”. However, we need to feed into the function each array item to be evaluated. In the olden days we would create a for loop. Today we have something better thanks to the latest ECMAScript standard. We have filter().https://www.besanttechnologies.com/training-courses/php-training/php-training-institute-in-chennai

Jerry Stuckle

unread,
Jan 23, 2018, 7:50:51 AM1/23/18
to
On 1/23/2018 5:05 AM, inigoj...@gmail.com wrote:
> If we wanted to get all the even (or odd) numbers, we could use the above function “even”. However, we need to feed into the function each array item to be evaluated. In the olden days we would create a for loop. Today we have something better thanks to the latest ECMAScript standard. We have filter().https://www.besanttechnologies.com/training-courses/php-training/php-training-institute-in-chennai
>

Take your SPAM and stuff it up your arse. That way it can be right next
to your head.
0 new messages