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

array_filter with two conditions

15 views
Skip to first unread message

jans

unread,
Jan 4, 2018, 7:11:50 PM1/4/18
to
I am having trouble debugging this? I can't get it to print out the value if it is even or the word "odd" if it is odd? The initial funcitons work but I don't know how to use the array_filter with two conditions? thanks,
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);
$keys = array_keys($arr);

for ($counter = 0, $length = count($arr);$counter < $length; $counter++)
{
$key = $keys[$counter];
$value = $array[$key];
if ($arr_filter($arr,'even')==1){
print_r($array[$key];);
}elseif($arr_filter($arr,'odd')==1)
{
print_r('odd');
}
}

Jerry Stuckle

unread,
Jan 4, 2018, 8:03:14 PM1/4/18
to
See my reply to your previous message.

Also, people here volunteer their help; don't expect an answer right
away. We respond when we get on and have time. If you don't get an
answer within a couple of days then you can bump it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

Arno Welzel

unread,
Jan 4, 2018, 8:10:08 PM1/4/18
to
jans:

[...]
> if ($arr_filter($arr,'even')==1){
> print_r($array[$key];);
> }elseif($arr_filter($arr,'odd')==1)
> {
> print_r('odd');
> }

What is $arr_filter( ... )?

Also see:

<http://php.net/manual/en/function.array-filter.php>

And pay attention to the examples!


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

Christoph M. Becker

unread,
Jan 5, 2018, 7:29:48 AM1/5/18
to
On 05.01.2018 at 01:11, jans wrote:

> I am having trouble debugging this? I can't get it to print out the value if it is even or the word "odd" if it is odd? The initial funcitons work but I don't know how to use the array_filter with two conditions?

Why do you want to filter the array for this purpose? It seems to me
the simplest approach would be to iterate over the array, and then
conditionally printing what is desired. For instance:

<?php
$arr = array(1,2,6,7,9,4,10,11,12,3,13,24);
foreach ($arr as $val) {
echo ($val%2 ? "odd" : $val), PHP_EOL;
}

--
Christoph M. Becker

jans

unread,
Jan 8, 2018, 5:29:41 PM1/8/18
to
thanks for the explanation, after struggling with array_filter now I know when to use it.

Postiljon Petskin

unread,
Apr 22, 2023, 11:47:42 AM4/22/23
to
You agree, that You are the most unoriginal person in the history of the universe ?
0 new messages