Arrays on the fly

This is a discussion on Arrays on the fly within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi I'm sure there is a really easy way of doing this, but I just can't seem to ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-27-2007
Brian
 
Posts: n/a
Default Arrays on the fly

Hi

I'm sure there is a really easy way of doing this, but I just can't
seem to work it out.
I'm trying to use an array to create some more array which I
will then populate. It's going to be used to open up directory
and put of list of files in the array

Here is the top array

$galleries = array("array1" => "Descrption of 1", "array2" => "Descrption of
2", "array3" => "Descrption of 3");

foreach($galleries as $k => $v){
// do some tests of directory if all OK then create an array

$$k = array();

// add to above array here
}

I am am trying to end up with is 3 arrays called 1 to 3.


Thanks

B

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1444 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Reply With Quote
  #2 (permalink)  
Old 03-27-2007
ZeldorBlat
 
Posts: n/a
Default Re: Arrays on the fly

On Mar 27, 12:21 pm, "Brian" <brian_no_s...@nrwp.co.uk> wrote:
> Hi
>
> I'm sure there is a really easy way of doing this, but I just can't
> seem to work it out.
> I'm trying to use an array to create some more array which I
> will then populate. It's going to be used to open up directory
> and put of list of files in the array
>
> Here is the top array
>
> $galleries = array("array1" => "Descrption of 1", "array2" => "Descrption of
> 2", "array3" => "Descrption of 3");
>
> foreach($galleries as $k => $v){
> // do some tests of directory if all OK then create an array
>
> $$k = array();
>
> // add to above array here
>
> }
>
> I am am trying to end up with is 3 arrays called 1 to 3.
>
> Thanks
>
> B
>
> --------------------------------------------------------------------------------
> I am using the free version of SPAMfighter for private users.
> It has removed 1444 spam emails to date.
> Paying users do not have this message in their emails.
> Try SPAMfighter for free now!


That code worked fine for me. What exactly is the problem you're
having?

Reply With Quote
  #3 (permalink)  
Old 03-28-2007
Brian
 
Posts: n/a
Default Re: Arrays on the fly


"ZeldorBlat" <zeldorblat@gmail.com> wrote in message
news:1175021746.701819.274400@y80g2000hsf.googlegr oups.com...
> On Mar 27, 12:21 pm, "Brian" <brian_no_s...@nrwp.co.uk> wrote:
>> Hi
>>
>> I'm sure there is a really easy way of doing this, but I just can't
>> seem to work it out.
>> I'm trying to use an array to create some more array which I
>> will then populate. It's going to be used to open up directory
>> and put of list of files in the array
>>
>> Here is the top array
>>
>> $galleries = array("array1" => "Descrption of 1", "array2" => "Descrption
>> of
>> 2", "array3" => "Descrption of 3");
>>
>> foreach($galleries as $k => $v){
>> // do some tests of directory if all OK then create an array
>>
>> $$k = array();
>>
>> // add to above array here
>>
>> }
>>
>> I am am trying to end up with is 3 arrays called 1 to 3.
>>
>> Thanks
>>
>> B
>>

> That code worked fine for me. What exactly is the problem you're
> having?
>


Hi ZeldorBlat

Below is the full code, I get an error saying
"Fatal error: Cannot use [] for reading in /var/www/...."
on this line, it will not add to the array?

$$k[] = $file; // add file to array

The idea is I will build up a set of arrays with the file list in them
which I will then use to create a show the photos in the gallery

$galleries = array("array1" => "Descrption of 1", "array2" => "Descrption of
2", "array3" => "Descrption of 3");
$path = '/var/www/..../';


foreach($galleries as $k => $v){ // Loop though each Key and Value in
gallery array
$gal_path = $path.$k; // make path to gallery
if ($handle = opendir($gal_path)) { // open path
$$k = array(); // make new array using the key as a
name
while (false !== ($file = readdir($handle))) {
if (is_file($k.'/'.$file)) { // check if is a file
$$k[] = $file; // add file to array
print "the file is $file<br>"; // print test line, remove later
}
}
closedir($handle);
sort($$k);
reset($$k);
} else {
print "<h3 align=center>$k Thumbnail Directory could not be opened,
sorry</h3>";
}
} // LOOP END foreach galleries



--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1463 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Reply With Quote
  #4 (permalink)  
Old 03-28-2007
ZeldorBlat
 
Posts: n/a
Default Re: Arrays on the fly

On Mar 27, 8:18 pm, "Brian" <brian_nosp...@nrwp.co.uk> wrote:
> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message
>
> news:1175021746.701819.274400@y80g2000hsf.googlegr oups.com...
>
>
>
> > On Mar 27, 12:21 pm, "Brian" <brian_no_s...@nrwp.co.uk> wrote:
> >> Hi

>
> >> I'm sure there is a really easy way of doing this, but I just can't
> >> seem to work it out.
> >> I'm trying to use an array to create some more array which I
> >> will then populate. It's going to be used to open up directory
> >> and put of list of files in the array

>
> >> Here is the top array

>
> >> $galleries = array("array1" => "Descrption of 1", "array2" => "Descrption
> >> of
> >> 2", "array3" => "Descrption of 3");

>
> >> foreach($galleries as $k => $v){
> >> // do some tests of directory if all OK then create an array

>
> >> $$k = array();

>
> >> // add to above array here

>
> >> }

>
> >> I am am trying to end up with is 3 arrays called 1 to 3.

>
> >> Thanks

>
> >> B

>
> > That code worked fine for me. What exactly is the problem you're
> > having?

>
> Hi ZeldorBlat
>
> Below is the full code, I get an error saying
> "Fatal error: Cannot use [] for reading in /var/www/...."
> on this line, it will not add to the array?
>
> $$k[] = $file; // add file to array
>
> The idea is I will build up a set of arrays with the file list in them
> which I will then use to create a show the photos in the gallery
>
> $galleries = array("array1" => "Descrption of 1", "array2" => "Descrption of
> 2", "array3" => "Descrption of 3");
> $path = '/var/www/..../';
>
> foreach($galleries as $k => $v){ // Loop though each Key and Value in
> gallery array
> $gal_path = $path.$k; // make path to gallery
> if ($handle = opendir($gal_path)) { // open path
> $$k = array(); // make new array using the key as a
> name
> while (false !== ($file = readdir($handle))) {
> if (is_file($k.'/'.$file)) { // check if is a file
> $$k[] = $file; // add file to array
> print "the file is $file<br>"; // print test line, remove later
> }
> }
> closedir($handle);
> sort($$k);
> reset($$k);
> } else {
> print "<h3 align=center>$k Thumbnail Directory could not be opened,
> sorry</h3>";
> }
> } // LOOP END foreach galleries
>
> --------------------------------------------------------------------------------
> I am using the free version of SPAMfighter for private users.
> It has removed 1463 spam emails to date.
> Paying users do not have this message in their emails.
> Try SPAMfighter for free now!


And what does the line that is referenced in the message "Fatal error:
Cannot use [] for reading in /var/www/...." look like?

Reply With Quote
  #5 (permalink)  
Old 03-28-2007
Geoff Berrow
 
Posts: n/a
Default Re: Arrays on the fly

Message-ID: <5viOh.22881$0Z1.21461@newsfe7-win.ntli.net> from Brian
contained the following:

>Below is the full code, I get an error saying
>"Fatal error: Cannot use [] for reading in /var/www/...."
>on this line, it will not add to the array?
>
>$$k[] = $file; // add file to array


Variable variables doesn't work with arrays like this. I solved it by
using a multi dimensional array but some people posted some alternatives
on one of the groups recently. (solutions involved curly brackets I
think)

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Reply With Quote
  #6 (permalink)  
Old 03-28-2007
Brian
 
Posts: n/a
Default Re: Arrays on the fly

>>Below is the full code, I get an error saying
>>"Fatal error: Cannot use [] for reading in /var/www/...."
>>on this line, it will not add to the array?
>>
>>$$k[] = $file; // add file to array


Geoff Wrote

> Variable variables doesn't work with arrays like this. I solved it by
> using a multi dimensional array but some people posted some alternatives
> on one of the groups recently. (solutions involved curly brackets I
> think)


Hi Geoff

Thanks for the replay, yes curly brackets did
work, the line was changed to

${$k}[] = $file;


Many thanks

Brain






--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1505 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:03 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0