Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #17637 > unrolled thread

array_filter with two conditions

Started byjans <janis.rough@gmail.com>
First post2018-01-04 16:11 -0800
Last post2023-04-22 08:47 -0700
Articles 6 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  array_filter with two conditions jans <janis.rough@gmail.com> - 2018-01-04 16:11 -0800
    Re: array_filter with two conditions Jerry Stuckle <jstucklex@attglobal.net> - 2018-01-04 20:03 -0500
    Re: array_filter with two conditions Arno Welzel <usenet@arnowelzel.de> - 2018-01-05 02:10 +0100
    Re: array_filter with two conditions "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-01-05 13:29 +0100
      Re: array_filter with two conditions jans <janis.rough@gmail.com> - 2018-01-08 14:29 -0800
    Re: array_filter with two conditions Postiljon Petskin <yyyyyyyyyyyywwwww@zohomail.eu> - 2023-04-22 08:47 -0700

#17637 — array_filter with two conditions

Fromjans <janis.rough@gmail.com>
Date2018-01-04 16:11 -0800
Subjectarray_filter with two conditions
Message-ID<fe3760f6-83ed-47b3-8402-880fd3d26408@googlegroups.com>
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');
      }
}

[toc] | [next] | [standalone]


#17639

FromJerry Stuckle <jstucklex@attglobal.net>
Date2018-01-04 20:03 -0500
Message-ID<p2misc$c6f$2@jstuckle.eternal-september.org>
In reply to#17637
On 1/4/2018 7:11 PM, 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?  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');
>        }
> }
> 

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
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#17641

FromArno Welzel <usenet@arnowelzel.de>
Date2018-01-05 02:10 +0100
Message-ID<fb81jaFpc8eU2@mid.individual.net>
In reply to#17637
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

[toc] | [prev] | [next] | [standalone]


#17642

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2018-01-05 13:29 +0100
Message-ID<p2nr3h$u2c$1@solani.org>
In reply to#17637
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

[toc] | [prev] | [next] | [standalone]


#17645

Fromjans <janis.rough@gmail.com>
Date2018-01-08 14:29 -0800
Message-ID<e43ef2b6-5b05-4f35-bcdf-475994ce4bac@googlegroups.com>
In reply to#17642
On Friday, January 5, 2018 at 4:29:48 AM UTC-8, Christoph M. Becker wrote:
> 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

thanks for the explanation, after struggling with array_filter now I know when to use it.

[toc] | [prev] | [next] | [standalone]


#19444

FromPostiljon Petskin <yyyyyyyyyyyywwwww@zohomail.eu>
Date2023-04-22 08:47 -0700
Message-ID<f8b78183-a441-4e48-a45b-b2978006fa68n@googlegroups.com>
In reply to#17637
On Friday, January 5, 2018 at 2:11:50 AM UTC+2, 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? 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'); 
> } 
> }

You agree, that You are the most unoriginal person in the history of the universe ?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web