Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 嵌套Foreach循环_Php - Fatal编程技术网

Php 嵌套Foreach循环

Php 嵌套Foreach循环,php,Php,我是php新手。我有一个foreach循环,它检查一张票证并将该票证数据输出到google电子表格中。另一个foreach循环存储旧票证id,但我需要将新票证id与旧票证id数组进行比较,如果存在,则删除它并添加新票证数据。我在比较旧的票证id和新的票证id时遇到问题,请告知如何做到这一点。谢谢 foreach ($result->tickets as $ticket) { // For each for old ticket id's foreach ($resu

我是php新手。我有一个foreach循环,它检查一张票证并将该票证数据输出到google电子表格中。另一个foreach循环存储旧票证id,但我需要将新票证id与旧票证id数组进行比较,如果存在,则删除它并添加新票证数据。我在比较旧的票证id和新的票证id时遇到问题,请告知如何做到这一点。谢谢

  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;
  }

提前谢谢。

你应该这样做

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;
}

你应该这样做

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;
}

按如下方式重写代码:

  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;
  }
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

按如下方式重写代码:

  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;
  }
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

这是一个很难回答的问题,因为你不太清楚你想要完成什么

通常,嵌套的foreach如下所示:

  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;
  }
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

这是一个很难回答的问题,因为你不太清楚你想要完成什么

通常,嵌套的foreach如下所示:

  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;
  }
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

您可以将旧票证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.
}

嗯,我看这和安东的答案是一样的。:-)

您可以将旧票证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.
}

嗯,我看这和安东的答案是一样的。:-)

你的问题到底是什么?为什么在完全相同的结果集中循环两次?您是否也在原始代码中执行此操作…?目前,您的循环毫无意义。什么是新票,什么是旧票?为什么你只有一个$result?您是否应该没有$new\u ticket\u result和$old\u ticket\u result?如果要检查当前新票证是否存在于旧票证数组中,那么应该在第二个for循环中进行检查。也许对你有帮助。对于如何实现嵌套的foreach,您似乎有点困惑。您的问题是什么?为什么在完全相同的结果集中循环两次?您是否也在原始代码中执行此操作…?目前,您的循环毫无意义。什么是新票,什么是旧票?为什么你只有一个$result?您是否应该没有$new\u ticket\u result和$old\u ticket\u result?如果要检查当前新票证是否存在于旧票证数组中,那么应该在第二个for循环中进行检查。也许对你有帮助。对于如何实现嵌套的foreach,您似乎有点困惑