0

我是 php 新手。我有一个 foreach 循环,用于检查票并将此票数据输出到谷歌电子表格中,另一个 foreach 循环存储旧票 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;
  }

提前致谢。

4

5 回答 5

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
于 2012-05-08T09:35:31.403 回答
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;
}
于 2012-05-08T09:32:24.977 回答
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.
}

好吧,我看到这与安东的答案相同。:-)

于 2012-05-08T10:00:55.697 回答
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? 
}
于 2012-05-08T09:42:02.853 回答
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;
  }
于 2012-05-08T09:34:53.643 回答