J
J
Jony13372016-11-20 10:40:51
PHP
Jony1337, 2016-11-20 10:40:51

Why doesn't iterate over and split an array in php?

There is an array like

Array ( [uid] => 5551577722424 [name] => Ana [gender] => female [pic_3] => https://i.mycdn.me/image?id=839846908062&bid=848695320478&t=33&plc=API&ts=000000004d011304a5&viewToken=fcizknwfq6YEP4ma8idLww&aid=1246684928&tkn )

It has many elements, this is a list of a person's friends, name => name of a person, gender => gender, pic_3 => this is a link to a photo
I want to separate people by gender
, that is, I did this
// $count - число элементов массива
for ($i = 0 ; $i <= $count; $i++) {

if ($two_friend [$i] ['gender'] == 'female') {
  $femaleGender   = $two_friend[$i]; 

}
else {
  $MaleGender  = $two_friend[$i]; 
}
}

Purely in theory, men go to the $MaleGenger array, and women go to the femaleGender array.
When I do print_r ($femaleGender) to display only the last element, it means that only the last one is added, why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
roswell, 2016-11-20
@Jony1337

$femaleGender = array();
$maleGender = array();
for ( $i = 0 ; $i <= $count ; $i++ ) {
    if ( $two_friend[ $i ][ 'gender' ] == 'female' ) {
        $femaleGender[] = $two_friend[ $i ];
    } else {
        $maleGender[] = $two_friend[ $i ]; 
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question