I have an array of objects:
$obj = array();
// add the new items
$row = new stdClass();
$row->first = $first;
$row->last = $last;
$row->phone = $phone;
$obj[] = $row;
Now, if I only have the value of $last, is there a way to delete the entire $row object without specifying each key/value? (If it helps to understand, if it was a mysql statement it would be something like "DELETE * FROM $obj WHERE $row->last = 'Thomas' ")
thx
You need to know the key. However, if you iterate through the array you can find it.
/* Your original code: */
$obj = array();
// add the new items
$row = new stdClass();
$row->first = $first;
$row->last = $last;
$row->phone = $phone;
$obj[] = $row;
/* My addition: */
$theOneToDelete = $last;
foreach ($obj as $key => $row) {
if ($row->last == $theOneToDelete) {
unset($obj[$key]);
break;
}
}
There may be a terser approach with some of the array_*
functions, but this lays it out.
Is unset($row);
what you want? But there is no need to do that if you are just doing it to save memory...
foreach ($obj as $key => $value) {
if($obj[$key]->last == $last){
unset($obj[$key]);
}
}
Oops, I forgot to format my response as code. I'm new here! =P