removing an array from a Multidimesional Array
Moderator: General Moderators
removing an array from a Multidimesional Array
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.
as always there is more than one way 
with unset you will leave no 'hole' in the array but the indicies. trybut 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
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);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
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
i loaded this into my "test.php":
and this came up:
just so you would know it doesn't actually REMOVE the old array, it just removes the key.
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>";
?>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
)How do I reference the first array
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.
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.