Page 1 of 1
removing an array from a Multidimesional Array
Posted: Sun Aug 11, 2002 6:30 pm
by SilvrWolf
I have created array (which have 3 variables) I then contain all of these arrays in one main array. My question is thus. if it becomes necessary to remove one of those arrays from the main array how do I do it and not leave an empty pointer in the main array. Like a stack I may want to remove one plate and have everything above shift accordingly.
Posted: Sun Aug 11, 2002 6:42 pm
by volka
as always there is more than one way

with
unset you will leave no 'hole' in the array but the indicies. try
Code: Select all
$arr = array('a','b','c','d'); // may be arrays, too
unset($arrї2]);
print_r($arr);
but as long as you're using
foreach it is the fastest way.
With
array_splice you can cut a piece out of an array and the rest will be re-indexed (right?)
If you're going to delete more than one entry by a specific criteria take a look at
array_filter. The remaining entries will be re-indexed as well
Posted: Sun Aug 11, 2002 7:08 pm
by hob_goblin
i loaded this into my "test.php":
Code: Select all
<?
$_1 = array('1', '2', '3');
$_2 = array('4', '5', '6');
$_3 = array('7', '8', '9');
$fl = array($_1, $_2, $_3);
echo "<pre>";
print_r($fl);
echo "</pre>";
unset($flї1]);
echo "<pre>";
print_r($fl);
print_r($_2);
echo "</pre>";
?>
and this came up:
Code: Select all
Array
(
ї0] => Array
(
ї0] => 1
ї1] => 2
ї2] => 3
)
ї1] => Array
(
ї0] => 4
ї1] => 5
ї2] => 6
)
ї2] => Array
(
ї0] => 7
ї1] => 8
ї2] => 9
)
)
Array
(
ї0] => Array
(
ї0] => 1
ї1] => 2
ї2] => 3
)
ї2] => Array
(
ї0] => 7
ї1] => 8
ї2] => 9
)
)
Array
(
ї0] => 4
ї1] => 5
ї2] => 6
)
just so you would know it doesn't actually REMOVE the old array, it just removes the key.
Posted: Sun Aug 11, 2002 7:43 pm
by volka
of course.
Everything in php is either reference-counted or copy-based.
As long as one reference is up the content is up

if you want the whole content to be marked as 'destroyable' you must also unset $_2 (or reset $_2 to something else i.e. $_2 = 2; )
How do I reference the first array
Posted: Wed Aug 14, 2002 2:40 pm
by SilvrWolf
Thank you for the help all. I got the unset working. heres my next question.
Please look at the code below
foreach($student_list as $student_value) {
echo "<tr><td align=\"left\" width=\"25%\">".$student_value['Student_Name']."</td>\n
<td align=\"left\" width=\"25%\">".$student_value['Student_Phone']."</td>\n
<td align=\"left\" width=\"25%\">".$student_value['Student_Email']."</td>\n";
echo "<td align=\"left\" width=\"25%\"><a href=\"".$PHP_SELF."?action=delete_student&student_id=".???????."\" onClick=\"return confirm('Are you sure you want to Remove this Student? This will delete their information for this database?')\">Delete</a></td>\n
</tr>";
}
how do I reference the array that contains the student data I want to unset? I need a reference point to place where the ?????? is.