如何使用php根据条件复制数组

如何使用php根据条件复制数组,php,arrays,Php,Arrays,我有一个数组,它从数据库中选择staff_id并在表中随机显示它们 但是我想根据staff_级别复制数组,这样初级人员将被复制,而高级人员将不会被复制 这是为了使初级职员比高级职员出现两次。代码如下: foreach ($invigilator_array_indexes as $array_index) { if ($row['level'] == 1){ $invi_array = $invigilators[$array_index]

我有一个数组,它从数据库中选择staff_id并在表中随机显示它们

但是我想根据staff_级别复制数组,这样初级人员将被复制,而高级人员将不会被复制

这是为了使初级职员比高级职员出现两次。代码如下:

foreach ($invigilator_array_indexes as $array_index) {
            if ($row['level'] == 1){
                $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                $output .=$invi_array;
                $output .=$invi_array.concat();
            }else {
                $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                $output .=$invi_array;
            }

    }
请注意:

foreach ($invigilator_array_indexes as $array_index) {
                $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                $output .=$invi_array;
    }
最初以随机顺序输出staff_id

欢迎所有帮助,我们将不胜感激。谢谢你的期待

完整代码:

<?php
$page = isset($_GET['page']) ? $_GET['page'] : 1;


$records_per_page = 50;

$from_record_num = ($records_per_page * $page) - $records_per_page;

include_once 'includes/config.php';
include_once 'includes/data.inc_course.php';
include 'includes/shuffle2.php';


$database = new Config();
$db = $database->getConnection();

$product = new Data($db);

$stmt = $product->readAll($page, $from_record_num, $records_per_page);
$num = $stmt->rowCount();

//range declaration
$range = $_POST['range'];

?>



<?php include('includes/header.php'); 



$output1 = "

    <body>
        <div id='wrapper'>";

        echo $output1;

       include('includes/nav.php');

$output2 = "
       <!-- Page Content -->
        <div id='page-wrapper'>
            <div class='container-fluid'>
                <div class='row'>
                    <div class='col-lg-12'>
                        <h1 class='page-header'>Timetable Details</h1>
                    </div>
                    <!-- /.col-lg-12 -->

                    <!-- niyicode -->
                <div class='col-lg-12'>
                    <p>
                        <a class='btn btn-primary' href='courses/add.php' role='button'>Add Course</a>
                        <a class='btn btn-primary fleft' href='staff/add.php' role='button'>Add Staff</a>
                    </p>";
        echo $output2;

        if($num>0){
            if($range>0){


            $query1 = "SELECT staffid_pdo,level FROM staff_list WHERE avail_pdo = 1 ORDER BY id_pdo ASC ";



            $invi_stmt = $db->prepare($query1);
            $invi_stmt->execute();
            $invigilators = $invi_stmt->fetchAll(PDO::FETCH_ASSOC);

            //generate an array containing the range of keys in the invigilators resultset we got above
            $invigilators_array_range = range(0, (count($invigilators) - 1));

$output =  "           
        <table class= 'table table-bordered table-hover table-striped js-serial' id='printTable'>
            <caption>Timetable with number of students</caption>
            <thead>
                <tr class='success'>
                    <th style='width: 4%'>#</th>
                    <th>No. Students</th>
                    <th>Code</th>
                    <th>Date</th>
                    <th>Start Time</th>
                    <th>End Time</th>
                    <th>Venue</th>
                    <th>Duration</th>
                    <th>Invigilator</th>
                    <!-- <th>Edit/Del</th> -->
                </tr>
            </thead>
        <tbody>

";
        // echo $output;


while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    extract($row);


    $invigilators_needed = round(($row['snb_pdo'] / $range), 0, PHP_ROUND_HALF_EVEN);
    print_r($invigilators_needed);
    $date_active = $row['dat_pdo'];

    $output .= ' <tr>' ;
    $output .= '' ;
    $output .= '   <td>'.$row['snb_pdo'].'</td>' ;
    $output .= '   <td>'.$row['coscd_pdo'].'</td>' ;
    $output .= '   <td>'.$date_active.'</td>' ;
    $output .= '   <td>'.$row['starttim_pdo'].'</td>' ;
    $output .= '   <td>'.$row['endtim_pdo'].'</td>' ;
    $output .= '   <td>'.$row['ven_pdo'].'</td>' ;
    $output .= '   <td>'.$row['dur_pdo'].'</td>' ;
    $output .= '   <td>';

    $invigilator_array_indexes = array_rand($invigilators_array_range, $invigilators_needed);

    if (count($invigilator_array_indexes)) {
        foreach ($invigilator_array_indexes as $array_index) {
                if ($row['level'] == 1){
                    $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                    $output .=$invi_array;
                    $output .=$invi_array.clone();
                }else {
                    $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                    $output .=$invi_array;
                }

                print_r($invi_array);

        }

    } else $output .= "
        <div class='alert alert-danger' role='alert'>
          <button type='button' class='close' data-dismiss='alert' aria-label='Close'></button>
           Not enough <strong>invigilators!</strong>
        </div>
        ";
    $output .= '</td>' ;
    // Add more here if appropriate .... ;
    $output .= ' </tr>' ;
?>
                    <?php
// }
}
$output .='      </tbody>
    </table>    
';
$results->free();
mysqli_close($mysqli);
echo $output;


$page_dom = "index.php";
include_once 'includes/pagination.inc_course.php';
?>

<center><button onclick="printData();" class="btn btn-primary hideprint"> Print </button></center>
<br/>

<?php
//range ending
    } else {
        ?>
                    <div class="alert alert-warning alert-dismissible" role="alert">
                      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                      <strong>Warning!</strong> No valid range entered. Please enter a valid range <a href="staff/range.php"> here! </a>
                    </div>
                    <?php
                    }
                    ?>
<?php
//if num ending
}
else{
?>
                    <div class="alert alert-warning alert-dismissible" role="alert">
                      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                      <strong>Warning!</strong> No Data. Please add data to view timetable
                    </div>
                    <?php
                    }
                    ?>
<?php   
$output5 = "                       </div>
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
    ";
        echo $output5;


?>
<!-- echo $output;                     -->
    <?php include('includes/footer.php'); ?>


$duplicatedArray=$originalArray
@Nordenheim请解释一下,数组对于我来说是一个令人困惑的话题,请发布完整的代码,而不是部分代码。
$invisilator\u array\u index
$row
$invisilators
,有什么内容?@Bankzilla我添加了完整的代码您从未显示上述值中的内容。另外,我建议不要使用
extract()
,因为您甚至没有使用从中产生的任何变量。看起来您只需要进行基本调试,以查看在确定目标之前有哪些信息。