嵌套的Foreach循环

时间:2012-05-08 09:29:08

标签: php

我是php的新手。我有一个foreach循环,用于检查故障单并将此故障单数据输出到谷歌电子表格中,其他foreach循环存储oldticket id但我需要将新故障单ID与旧故障单数组进行比较,如果存在则删除它并添加新票证数据。我有一个问题,比较旧的机票ID和新的1请告知如何做到这一点。感谢

  foreach ($result->tickets as $ticket)
  {
    // For each for old ticket id's
    foreach ($result->tickets as $oldticket)
    {
           $oldid = $oldticket->id;
    }

    $row = array
    (
          "Ticket ID" => $ticket->id,
          "Ticket Title" => $ticket->title,
          "Created On" => $month_created_on,
          "Solved On" => $solved_on ,
          "Status" => $ticket->status,
          "Time Spent" => $ticket->time_spent,
          "Assigned To" => $ticket->assigned_to->full_name,
          "Mojo Number" => $ticket->assigned_to->mojo_number 
      );

    if ($ticket->id ==  $oldid){
                $ss->deleteRow($row);
    }
    $ss->addRow($row);

    return $row;
  }

提前致谢。

5 个答案:

答案 0 :(得分:2)

这是一个难以回答的问题,因为你要完成的目标还不是很清楚。

通常,嵌套的foreach看起来像这样:

foreach($result as $key => $value){
    foreach($value as $subKey => $subValue){

       //code here while still being able to access $key,$value from the top foreach

     }//foreach

     //code here what you want to do per lower level grouping, ie the second foreach

}//foreach

答案 1 :(得分:1)

你应该做这样的事情

foreach ($result->tickets as $ticket)
{
    // For each for old ticket id's
    foreach ($ticket as $oldticket)
    {
        $oldid = $oldticket->id;
    }

    $row = array
    (
        "Ticket ID" => $ticket->id,
        "Ticket Title" => $ticket->title,
        "Created On" => $month_created_on,
        "Solved On" => $solved_on ,
        "Status" => $ticket->status,
        "Time Spent" => $ticket->time_spent,
        "Assigned To" => $ticket->assigned_to->full_name,
        "Mojo Number" => $ticket->assigned_to->mojo_number 
    );

    if ($ticket->id ==  $oldid){
        $ss->deleteRow($row);
    }
    $ss->addRow($row);

    return $row;
}

答案 2 :(得分:1)

重写你的代码:

  foreach ($result->tickets as $ticket)
  {

    $row = array
    (
          "Ticket ID" => $ticket->id,
          "Ticket Title" => $ticket->title,
          "Created On" => $month_created_on,
          "Solved On" => $solved_on ,
          "Status" => $ticket->status,
          "Time Spent" => $ticket->time_spent,
          "Assigned To" => $ticket->assigned_to->full_name,
          "Mojo Number" => $ticket->assigned_to->mojo_number 
      );
    // For each for old ticket id's
    foreach ($result->tickets as $oldticket)
    {
        $oldid = $oldticket->id;

        if ($ticket->id ==  $oldid){
                $ss->deleteRow($row);
        }
    }
    $ss->addRow($row);

    return $row;
  }

答案 3 :(得分:1)

$aOld = array(); // array to save your old tickets.

foreach ($result->tickets as $ticket)
{
  $aRow = array(
      "Ticket ID" => $ticket->id,
      "Ticket Title" => $ticket->title,
      "Created On" => $month_created_on,
      "Solved On" => $solved_on ,
      "Status" => $ticket->status,
      "Time Spent" => $ticket->time_spent,
      "Assigned To" => $ticket->assigned_to->full_name,
      "Mojo Number" => $ticket->assigned_to->mojo_number 
  );
  if (in_array($ticket->id, $aOld)){ // if we had such id, so delete this row, else add it
    $ss->deleteRow($row);
  } else {
    $ss->addRow($row);
    $aOld[] = $ticket->id;
  }
//  return $row; Why this is here? 
}

答案 4 :(得分:1)

您可以将旧票证ID值保留在一个数组中。 然后使用in_array()函数,如下所示:

//Compose old ticket group.
$old_tickets = array();

// Add an old ticket id into old ticket id group
$old_tickets[] = $old_ticket_id;

//When to fetched new ticket ids, inside of loop, check if ticket id is already existing $old_tickets

if(in_array($ticket_id, $old_tickets)) { 
    // Do something, remove a row process in your case.
}

嗯,我看到这与Anton的答案相同。 : - )