Reputation:
I am new to php. I have a foreach loop that checks for a ticket and outputs this ticket data into a google spreadsheet the other foreach loop stores the oldticket ids but i need to compare the new ticket id with the old ticket id array and if it exists delete it and add the new ticket data. Im having a problem comparing the old ticket id with the new 1 please advise on how this can be done. Thanks
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;
}
Thanks in advance.
Upvotes: 0
Views: 1991
Reputation: 509
You should do something like this
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;
}
Upvotes: 1
Reputation: 3189
You can keep old ticket id values into one array. And then use in_array() function like following:
//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.
}
Well, and I see this is same answer with Anton's one. :-)
Upvotes: 1
Reputation: 1061
$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?
}
Upvotes: 1
Reputation: 329
This is a tough one to answer because it's not very clear what you're trying to accomplish.
Normally, a nested foreach looks like this:
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
Upvotes: 2
Reputation: 12535
Rewrite your code like this:
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;
}
Upvotes: 1