PHP数组按数组内的值删除

时间:2016-07-20 08:56:55

标签: php

有一系列机场充满了用户列表。

    $airport_array[$airport_row['airportICAO']]    = array(
        'airportName' => $airport_row['airportName'],
        'airportCity' => $airport_row['airportCity'],
        'airportLat' => $airport_row['airportLat'],
        'airportLong' => $airport_row['airportLong'],
        'airportUserCount' => 0,
        'airportUserList' => array()
    );

填写后," airportUserCount"将是0或高于1.现在,我想从airportUserCount设置为0的阵列中删除所有机场。什么是最高性能的方法?我想到了一个foreach循环,但我担心它不一定是最优雅的解决方案。

5 个答案:

答案 0 :(得分:2)

foreach循环,检查具有Count == 0的那些,然后从数组中删除它们。

using (var context = new MICSYDBContext())
{
    var Doc = context.DOCUMENTS.OrderBy(x => x.ID).Take(100);

    try
    {
        using (var context2 = new BRIA_REALDBCONTEXT())
        {
            Stopwatch sw = Stopwatch.StartNew();

            foreach (var item in Doc)
            {
              var DOC = new DLV_DOC();
              DOC.FOLDID = item.ID;
              DOC.FOBJECT = 1;
              DOC.FIO = 40;              
              DOC.FID = newId;
              DOC.FACTIVE = "Y";
              DOC.FCODEWRT = 85;
              DOC.FDOC_COUNT_ORIGINAL = 'N';
              DOC.FUSERINSERT = 85;
              DOC.FMODULE = 1;
              DOC.FSERVICE_TYPE = 1;

              var micsysserviceName = context.DEFDOCTYPE.Where(x => x.ID == item.DOCTYPE).FirstOrDefault();

              if (micsysserviceName != null)
              {
                var service = context2.DLV_SSERVICE.Where(x => x.FNAME == micsysserviceName.NAME && x.FSERVICE_TYPE == 1).FirstOrDefault();

                if (service != null)
                {
                  DOC.FSERVICE = service.FID;
                }
              }
              else
              {
                DOC.FSERVICE = 1;
              }

              var personcorespondent = context2.UCM_PERSON.Where(x => x.FOLD_ID == item.DOCKORID.ToString()).FirstOrDefault();

              if (personcorespondent != null)
              {
                DOC.FPERSON = personcorespondent.FID;
              }
              else
              { 
                  DOC.FPERSON = 142; 
              }

              DOC.FDATEDOC = item.DATA;

              foreach (var docsteps in context.DOCMOVE.Where(x => x.DOCID == item.ID).OrderBy(x => x.DATA))
              {//СТъпка
                DLV_DOC_STEP DLVSTEP = new DLV_DOC_STEP();
                DLVSTEP.FDOC = DOC.FID;
                DLVSTEP.FOBJECT = 1;
                DLVSTEP.FCODEWRT = 85;


                var whofirst = context2.UCM_PERSON.Where(x => x.FOLD_ID == docsteps.WHOFIRST.ToString()).FirstOrDefault();
                if (whofirst != null)
                {
                  DLVSTEP.FPERSON = whofirst.FID;
                }
                var whonext = context2.UCM_PERSON.Where(x => x.FOLD_ID == docsteps.WHONEXT.ToString()).FirstOrDefault();
                if (whonext != null)
                {
                  DLVSTEP.FTOPERSON = whonext.FID;
                }

                context2.DLV_DOC_STEP.Add(DLVSTEP);
              }

              context2.DLV_DOC.Add(DOC);
              i++;
              Console.WriteLine("Added " +i + "DOC");
            }
            context2.SaveChanges();

            Console.WriteLine("End of docs");
            Console.WriteLine(sw.Elapsed.Hours);
          }
        }
        catch (Exception ex)
        {
          string s = ex.Message;
          Console.WriteLine("Problem with record");
          Console.WriteLine(ex.Message);
          Console.WriteLine(ex.StackTrace);
          Console.WriteLine(ex.InnerException);
        }
      }

答案 1 :(得分:2)

使用此代码

 foreach($airport_array as $key=>$value){
      if($value['airportUserCount']==0){
      unset($airport_array[$key]);
      }
 }

这是现场演示:https://eval.in/608462

答案 2 :(得分:2)

$new_airports = array_filter(
    $old_airports,
    function($a) { return 0 < $a['airportUserCount']; }
);

答案 3 :(得分:1)

使用array_filter

$a = array_filter($a, function($v) { return $v['airportUserCount'] != 0; });

演示: - https://eval.in/608464

答案 4 :(得分:1)

array_filter允许您在使用回调函数检查值时迭代数组。

function filterAirports($airports){
    return ($airport['airportUserCount'] == 0) ? true : false ;
}

print_r(array_filter($airport_array, "filterAirports"));