Re: Arrays
| From: | php at server dot mediawaveonline dot com | Date: | Wed, 13 Sep 2000 18:08:03 +0000 |
| Subject: | Re: Arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-16616@lists.php.net to get a copy of this message | ||
As long as you use dont need the index's to be numerically in order.
ie.
$array[0] = 'c';
$array[1] = 'h';
$array[2] = 'r';
$array[3] = 'i';
$array[4] = 's';
foreach ($array as $pos => $val)
{ // will display 'chris'
echo "$val \n";
}
unset $array[1];
foreach ($array as $pos => $val)
{ // will display 'cris'
echo "$val \n";
}
Chris Lee
On Sun, 13 Aug 2000, Glover George wrote:
> This may sound trivial, but its causing me a headache right now. When I
> first looked around in php and its similarities to c++ I noticed there are
> no std container classes (you know, like the standard template library).
> Specifically what I am looking for is the list data structure. The array
> seems to do most of the things the list does, such as poping and pushing and
> such. But why is there no way to delete an item from the middle of an
> array? I forgot the algorithm I used in c++ and that's what's causing me
> the headache. I know I can simply create a second array and copy only the
> values I want to keep into the temp array, and then after zeroing out the
> first array, copy the values back into it. However, that's kinda costly
> since I already have the worlds most "for" statements in a page :)
>
> So 2 questions:
>
> 1. Does anybody have a good algorithm for deleting a single item out of an
> array?
>
> 2. If I am going to use my method, once I have filled my temporary array
> with the wanted values, will somthing like the following work?
>
> $original_array = array_values($temp_array);
>
> or will I have to zero the original array out first?
>
> Thanks.
>
>
>