How to lineup random and mixed custom field type post on wordpress

I wanted to create a slider with mixed team members. So here I have used custom field as gender create two arrays for male and female members. Then I have created new array called $all_array to create an array what I need.

<?php

$all_array = array();
$female_array = array();
$male_array = array();
$argstop = array( ‘post_type’ => ‘teams’);
$female_argstop = array( ‘post_type’ => ‘teams’,’meta_key’,’gender’,’meta_value’ => ‘Female’, ‘orderby’ => ‘rand’,’post_status’ => ‘publish’);
$male_argstop = array( ‘post_type’ => ‘teams’,’meta_key’,’gender’,’meta_value’ => ‘Male’, ‘orderby’ => ‘rand’,’post_status’ => ‘publish’);
$female_teams = new WP_Query( $female_argstop );
$male_teams = new WP_Query( $male_argstop );
$female_posts = $female_teams->posts;
$male_posts = $male_teams->posts;
$female_count = count($female_posts);
$male_count = count($male_posts);

// create female team members array
for($i = 0; $i < $female_count; $i++){

foreach($female_posts[$i] as $key => $value) {

if($key === ‘ID’){
$female_array[$i][‘team_images’]= get_field(‘team_images’,$value);
}

$female_array[$i][$key]=$value ;
}
}

//create male team members array
for($j = 0; $j < $male_count; $j++){

foreach($male_posts[$j] as $key_m => $value_m) {

if($key_m === ‘ID’){
$male_array[$j][‘team_images’]= get_field(‘team_images’,$value_m);
}

$male_array[$j][$key_m]=$value_m ;
}
}

// Create merge array ..assume that
// female members are lesser than male members

for ($k = 0; $k < $female_count; $k++)
{
array_push($all_array,$female_array[$k]);
array_push($all_array,$male_array[$k]);
}

for ($x = $female_count; $x < $male_count; $x++)
{
array_push($all_array,$male_array[$x]);
}
?>

<div class=”container-fluid” id=”mobile-team”>
<div class=”row”>
<div class=”col-md-12">
<?php foreach($all_array as $team_members){ ?>
<div class=”col-md-4">
<div class=”post-img”><img src=”<?php echo $team_members[‘team_images’]; ?>” alt=”<?php the_title() ?>” /></div>
<div class=”team-member-name”><?php echo $team_members[‘post_title’]; ?></div>
<div class=”designation”><?php echo $team_members[‘post_excerpt’];?></div>
<div class=”team-desc”><?php echo $team_members[‘post_content’]; ?></div>
</div>

</div>

<?php } ?>

Computer Engineering(B.Tech) |Freelancer| music ❤ | Maths ❤ | Sudoku ❤