在条件下取消移动阵列

时间:2017-05-20 08:29:24

标签: php arrays multidimensional-array php-7 php-5.3

我写了下面的代码行

 public function shiftarray($cursor, $arg)
 {
     $keyarguments = array("first_name","roll_no");

     $arrayStudents =array();

       foreach ($cursor as $k => $row) 
       {
           foreach ($keyarguments as $key) 
            { 
                    if (strcasecmp($row[$key], $arg) == 0) 
                    {
                       array_unshift($arrayStudents, $row);
                    }
                    else
                    {

                    }
               }  
         } 
      return $arrayStudents;                                     
 } 

我有一些像$

这样的游标的学生
            { [0]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "Neil" ["middle_name"]=> string(4) "David" ["last_name"]=> string(6) "Stephan" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent@gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent@gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } }
         ...
         ...

我试图把这些学生带到阵列的开头,其名字是布莱恩,其余的学生应该在底部。

现在上面的代码将学生放在数组的顶部,但其余学生被忽略/删除。我不希望他们被删除。

我正在尝试调整要在其他情况下编写的代码。

请帮助!!!

2 个答案:

答案 0 :(得分:0)

在循环之前填充$ arrayStudents,否则只会收集和取消匹配。

扩展php.net/unshift的示例

$add = array('big');

$queue = array(
    array("orange"),
    array("banana"),
    array("apple1"),
    array("raspberry")
);

array_unshift($queue, $add);
print_r($queue);

在你的情况下:搜索你想要取消的钥匙,其余的转到标准的$ list = array(/ 学生列表 /):

$list = array();
$bringToTop = array('brian', 'elvis');
foreach($data as $item) {
    if (/* in in the list to bring on top*/) {
        $memory[] = $item;      
    } else {
        $list[] = $item;      
    }
}

foreach($memory as $item) {
   array_unshift($list, $item);
}
print_r($list);

亲切的问候

答案 1 :(得分:0)

$arrayStudents[] = $row;添加到您的其他范围。这是一个simple demo来说明它。

当匹配将元素添加到数组开头的结果时,不匹配会从数组的末尾添加它们。