removing an array from a Multidimesional Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SilvrWolf
Forum Newbie
Posts: 7
Joined: Sun Aug 11, 2002 11:00 am

removing an array from a Multidimesional Array

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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&#1111;1]); 

echo "<pre>"; 
print_r($fl); 
print_r($_2); 
echo "</pre>"; 

?>
and this came up:

Code: Select all

Array
(
    &#1111;0] => Array
        (
            &#1111;0] => 1
            &#1111;1] => 2
            &#1111;2] => 3
        )

    &#1111;1] => Array
        (
            &#1111;0] => 4
            &#1111;1] => 5
            &#1111;2] => 6
        )

    &#1111;2] => Array
        (
            &#1111;0] => 7
            &#1111;1] => 8
            &#1111;2] => 9
        )

)

Array
(
    &#1111;0] => Array
        (
            &#1111;0] => 1
            &#1111;1] => 2
            &#1111;2] => 3
        )

    &#1111;2] => Array
        (
            &#1111;0] => 7
            &#1111;1] => 8
            &#1111;2] => 9
        )

)

Array
(
    &#1111;0] => 4
    &#1111;1] => 5
    &#1111;2] => 6
)
just so you would know it doesn't actually REMOVE the old array, it just removes the key.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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; )
SilvrWolf
Forum Newbie
Posts: 7
Joined: Sun Aug 11, 2002 11:00 am

How do I reference the first array

Post 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.
Post Reply