Forum Moderators: coopster

Message Too Old, No Replies

problem unsetting array items

can do it using numbers but want to do it using names

         

HelenDev

3:54 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to do this...


unset($dirarray['css'], $dirarray['Templates']);

but it doesn't work, whereas this does


unset($dirarray[0], $dirarray[1]);

Any way I can get the former to work?

whoisgregg

4:15 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The simplified test case below works as expected on my test server, so I suspect it's either a server configuration issue or a problem with the surrounding code. Try running this code by itself:

<?php
$dirarray[] = '0 test';
$dirarray[] = '1 test';
$dirarray['css'] = 'css test';
$dirarray['Templates'] = 'Templates test';
echo '<pre>'; print_r($dirarray);
unset($dirarray['css'], $dirarray['Templates']);
unset($dirarray[0], $dirarray[1]);
print_r($dirarray); echo '</pre>';
?>

Does the second array still contain the string index array elements?

HelenDev

8:25 am on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks whoisgregg. The second array doesn't contain anything.

I've got a bit confused now, and perhaps I'm not going about things the right way...

Basically I've got an array from reading a list of all the directories in a location:


Array ( [0] => . [1] => .. [2] => css [3] => includes [4] => information for staff [5] => Templates [6] => information for students )

I want to get rid of certain items from this array, for example the Css directory, so I want something like this

foreach($dirarray as $diritem){
//if this == "Css" delete it from the array
//if this == "Templates" delete it from the array
}

Can anyone advise?

HelenDev

8:36 am on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yay! OK, I did it like this...

//remove any items we don't want from our array
$deletekey = array_search('css', $dirarray);
unset($dirarray[$deletekey]);
$deletekey = array_search('Templates', $dirarray);
unset($dirarray[$deletekey]);
$deletekey = array_search('.', $dirarray);
unset($dirarray[$deletekey]);
$deletekey = array_search('..', $dirarray);
unset($dirarray[$deletekey]);

I'm guessing there's a prettier (shorter) way to write this though?

whoisgregg

1:11 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What happened (I bet), is you tried to unset($diritem) inside your foreach loop? Foreach loops [php.net] don't reference the original array.

// fails, $diritem isn't a reference to the original array
foreach($dirarray as $diritem){
if($diritem == "css") unset($diritem);
}

$diritem is really just a copy of the value of the current array element... this foreach construct doesn't even give you a way of knowing what the original index was. However, there's a different way of writing foreach that gives you more power.

// works, $index gives us a way to go back and modify the original array
foreach($dirarray as $index=>$diritem){
if($diritem == "css") unset($dirarray[$index]);
}

You can use whatever variable names for $index and $diritem you'd like, many use

$k=>$v
or
$key=>$value
.

So back to the problem at hand. :) You have a known list of invalid directories, so put those in an array then loop through the dynamic list of directories and check each one against the invalid list. in_array [php.net] is a handy way to do this.

$dirarray = array( '.', '..', 'css', 'includes', 'information for staff', 'Templates', 'information for students');
$removearray = array( '.', '..', 'css', 'Templates');
foreach($dirarray as $k=>$v){
if(in_array($v, $removearray)){
unset($dirarray[$k]);
}
}
echo '<pre>'; print_r($dirarray); echo '</pre>';