How to foreach this?

I have this array:

Array
(
    [2] => Array
        (
            [sex] => 1
        )

    [295] => Array
        (
            [sex] => 1
        )

    [901] => Array
        (
            [sex] => 1
        )

    [1550] => Array
        (
            [sex] => 1
        )

    [1570] => Array
        (
            [sex] => 1
        )

    [1715] => Array
        (
            [sex] => 1
        )

    [1764] => Array
        (
            [sex] => 1
        )

    [1784] => Array
        (
            [sex] => 1
        )

    [1835] => Array
        (
            [sex] => 1
        )

    [1849] => Array
        (
            [sex] => 1
        )

    [1855] => Array
        (
            [sex] => 1
        )

    [1873] => Array
        (
            [sex] => 2
        )

    [1885] => Array
        (
            [sex] => 1
        )

    [1888] => Array
        (
            [sex] => 2
        )

    [1908] => Array
        (
            [sex] => 1
        )

    [1910] => Array
        (
            [sex] => 2
        )

    [1915] => Array
        (
            [sex] => 1
        )

    [1916] => Array
        (
            [sex] => 1
        )

)

I would like to output in other array IDs that have sex value 1???

array_filter(), array_keys()

Thanks buddy but how do I use this?

click on the links and scroll down to the examples’ section.

Please give me quick example please?

function odd($var)
{
    // returns whether the input integer is odd
    return($var & 1);
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);

echo "Odd :\n";
print_r(array_filter($array1, "odd"));

Thanks but I don’t see how I can apply it to my array? Please could you use my array in your example?

how about you give it a try yourself?

Buddy I am a newbie in PHP. I don’t get it please?

How do I remove array that has sex value 2 from this array

“newbie” doesn’t mean you shouldn’t attempt it yourself first.

1 Like

I am testing but I doesn’t seems to work!

from the Manual of array_filter():

Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array.

I am more visual learner please give me a hand please?

if you have code to test, you can post it here, so we can see where the problem is.

function odd($var)
{
    // returns whether the input integer is odd
    return($var & 1);
}

echo "Odd :\n";
print_r(array_filter($test, "odd"));


Odd :
Array
(
    [2] => Array
        (
            [sex] => 1
        )

    [295] => Array
        (
            [sex] => 1
        )

    [901] => Array
        (
            [sex] => 1
        )

    [1550] => Array
        (
            [sex] => 1
        )

    [1570] => Array
        (
            [sex] => 1
        )

    [1715] => Array
        (
            [sex] => 1
        )

    [1764] => Array
        (
            [sex] => 1
        )

    [1784] => Array
        (
            [sex] => 1
        )

    [1835] => Array
        (
            [sex] => 1
        )

    [1849] => Array
        (
            [sex] => 1
        )

    [1855] => Array
        (
            [sex] => 1
        )

    [1873] => Array
        (
            [sex] => 2
        )

    [1885] => Array
        (
            [sex] => 1
        )

    [1888] => Array
        (
            [sex] => 2
        )

    [1908] => Array
        (
            [sex] => 1
        )

    [1910] => Array
        (
            [sex] => 2
        )

    [1915] => Array
        (
            [sex] => 1
        )

    [1916] => Array
        (
            [sex] => 1
        )

)

Doesn’t remove array that have sex value 1

What is $var ???

why do you test for the odd-ness of each sub-array, when you want to test the value for the sex key?

$var is each array element that is tested.

I get no idea what I am doing?