0

I have this array returned from a query:


Array
(
    [0] => Array
        (
            [name] => 'Kathy'
            [gender] => 'female'
            [age] => 32
        )

    [1] => Array
        (
            [name] => 'Steve'
            [gender] => 'male'
            [age] => 19
        )

    [2] => Array
        (
            [name] => 'Joe'
            [gender] => 'male'
            [age] => 36
        )

    [3] => Array
        (
            [name] => 'Lynn'
            [gender] => 'female'
            [age] => 45
        )

)

I want to split this into two arrays: one containing only the females, and one containing only the males:

Is that possible to do with array functions, or will I have to make two queries (something I really don't want to do!)

7 Answers 7

2

Quick & dirty:

$females_array = array();
$males_array = array();

foreach($orginal_array as $array)
{
  if($array['gender'] == 'female')
  {
    $females_array[] = $array;
  }
  else
  {
   $males_array[] = $array;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@AgmLauncher if any of the answers provided solves your problem, you should mark it as "accepted" (by clicking the tick mark under the vote count on the top left corner of the answer), so that whoever reaches the answer in the future will spot at once the solution for the question. Thank you, cheers!
1

Sure you can! Let's say your array is $people, you can use:

$males = array();
$females = array();
foreach ($people as $person) {
    if ($person['gender'] == 'male') $males[] = $person;
    else $females[] = $person;
}

And then you have men in $males array and women in $females array.

Comments

1

You could quite easily do it manually.

$males = array();
$females = array();

foreach( $originalArray as $person ) {
    if( $person['gender'] === 'male' ) {
        $males[] = $person;
    } else {
        $females = $person;
    }
}

Comments

1

Just loop over, checking the value of gender key:

$array_male   = array();
$array_female = array();
foreach ($src_array as $elem) {
  if ($elem['gender'] == 'male') {
    $array_male[] = $elem;
  } else {
    $array_female[] = $elem;
  }
}

Comments

1

Wrote a small function for sorting people by gender. You could use switch inside instead of if/else, but since in current question there is only two criteria, then don't see any reason any reason to make code more clustered. This function should work fine for your problem.

function sortByGender( array $people )
{
    $result = array(
        'male'   => array(),
        'female' => array(),
    );

    foreach ( $people as $person )
    {
        if ( 'female' == $person['gender'] )
        {
            $result['female'] = $person;
        }
        else
        {
            $result['male'] = $person;
        }
    }

    return $result;
}

Comments

0

You need to write array function by your own. All Predefined PHP array functions are applicable to single dimension array only.

Comments

0

You could do this with the array_filter() function.

function check_female($arr) { return ($arr['gender'] == 'female'); }
function check_male($arr) { return ($arr['gender'] == 'male'); }

$males = array_filter($people, "check_male");
$females = array_filter($people, "check_female");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.