How to foreach this?

change the filter function to the following:

function odd($var)
{
    var_dump($var); // show what $var is
    return($var & 1);
}

OK see some progress!

Odd :
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "2"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "2"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "2"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}
array(1) {
  ["sex"]=>
  string(1) "1"
}

Now how do I make it remove every one what has value 1

preliminary question, if you had a variable $sex, how would you test it?

Foreach it or something I mean totally in the black never used this before?

have you ever worked with arrays before?

Nope buddy!

Only with ifs and sql and basic stuff!

then you should take the time to learn the array basics, you’ll need them over and over again:

http://php.net/array

Buddy I appreciate all that but could you PLEASE give me an example using my array I learn that way better PLEASE?

you only learn by doing things yourself. I know it’s painfully slow, but that’s the only way that works.

OK gonna google for it maybe get lucky :slight_smile:

Nailed it check this out baby!

foreach ($test as $key => $value) {
    
    foreach ($value as $row)
    {
    $id = "1";
    

    if ($row == $id) {
        unset($test[$key]);
    }
}
}
$persons = [
       2 => ['sex' => 1],
     295 => ['sex' => 1],
    1873 => ['sex' => 2],
     901 => ['sex' => 1],
];
$sex1 = array_filter($persons,function($person) {
    return $person['sex'] === 1 ? true : false;
});
var_dump($sex1);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.