Re: I should know this by now (?)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 25/06/2022 04:26, JEFFRY KILLEN wrote:

On Jun 24, 2022, at 7:21 PM, Anthony Pulse <inland14@xxxxxxxx> wrote:

Okay so you need to use

$_list as return from scandir will be an indexed array, No?
I could ignore $key because it would be a integer I suppose.

But I also believe the syntax would be
foreach($_list as $key => $value).
This is the way I have been doing it.



For($_list as $key => $value)

{

               //code goes here

               //increments and comparisons by $value and $key

}

Sent from Mail for Windows

From: JEFFRY KILLEN
Sent: Friday, June 24, 2022 10:16 PM
To: Anthony Pulse
Subject: Re: I should know this by now (?)

Thank you for the reference to array_keys.
I used it as count(array_keys(array)) and did not have any better result.
But that could be due to some other, yet unnoticed problem.

What I am trying to do is to keep from having to recreate a resource every
time a page loads.

In the page there are mp3 files references. The script in the top of the page
opens and reads a directory with the mp3 files. With that it writes to a javascript
source file.
So I have created a php script file with an index of the mp3 files. That is the associative
array. I can loop through and find file in the directory that are also in the index file. IF
the file count from scandir (minus skipped files) matches the file count in the index file,
the javascript file and php index file should not be rewritten. The index file and the array
from scandir will be different if the lengths do not match. That would be because an mp3
file has been removed and/or added.

private function mkChimeList()
                {
                 $_list = scandir('chimes');
                 require_once('chimes/chmIndex.php'); // contains $_chimes
                 $_validated = 0;
                 $_chimeList = [];
                 $_indexList = [];
                 for($_itr = 0; $_itr < count($_list); $_itr++)
                   {
                    switch($_list[$_itr])
                      {
                       case '..':
                       case '.':
                       case 'index.php':
                       case 'chmIndex.php':
                       break;
                       default;
                       if(isset($_chimes[$_list[$_itr]]))
                         {
                          $_validated++; //// incremented if the current item in $_list is found in the chimes array as a key.
                         }
                       $_chimeList[count($_chimeList)] = '"'.str_replace('.mp3', '', $_list[$_itr]).'":true';
                       $_indexList[count($_indexList)] = '"'.$_list[$_itr].'"=>true';
                       break;
                      }
                   }
                 $_updated = 'false';
                 if($_validated != (count($_chimes) - 4)) //// <<< here is the issue: this would mean that files were added and/or removed
                   {
                    @$_fileList = file_get_contents(self::$_chimeLib);
                    if($_fileList)
                      {
                       $_updated = 'true';
                       file_put_contents(self::$_chimeLib, 'const chimeList = {'.implode(', ', $_chimeList)."};\n");
                       $_index = file_get_contents('chimes/chmIndex.php');
                       file_put_contents('chimes/chmIndex.php', "<?php\n\$_chimes = [".implode(', ', $_indexList)."]; ?>");
                       return ['error'=>false, 'updated'=>$_updated];
                      }
                   }
                 return ['error'=>false, 'updated'=>$_updated];
                }

At present it is always running the update code ($_updated == true)

On Jun 24, 2022, at 6:03 PM, Ed Greenberg <edg@xxxxxxxxxxxxx> wrote:

You know, I have never wanted to count the number of key value pairs in an associative array.  Nonetheless, after you try it on a known associative array, if it doesn't work try the array_keys function. That will return in an ordered array of the keys in your associative array. You can certainly count those.

On Fri, Jun 24, 2022, 20:21 JEFFRY KILLEN <jekillen@xxxxxxxxxxx> wrote:
Hello:

Are associative arrays countable?

I am trying to process the  number of indexes in
an associative array and am having a real struggle
getting a proper comparison to an incremented numerical
variable.

I have looked at the manual entry for count() and that is
what is sparking the question because I do not see that
associative arrays are explicitly countable.

Thank you for time and attention
JK

On Jun 24, 2022, at 5:42 PM, Anthony Pulse <inland14@xxxxxxxx> wrote:

I would assume that using array_keys() would help
Sent from Mail for Windows From: JEFFRY KILLEN
Sent: Friday, June 24, 2022 8:21 PM
To: php-general General List
Subject: I should know this by now (?)
Hello:

Are associative arrays countable?

I am trying to process the  number of indexes in
an associative array and am having a real struggle
getting a proper comparison to an incremented numerical
variable.

I have looked at the manual entry for count() and that is
what is sparking the question because I do not see that
associative arrays are explicitly countable.

Thank you for time and attention
JK


You should have no problem with `count()` on the returned array from `scandir()`. As Aziz mentioned, `count()` works on any array or countable object. Speaking of, it might be worth looking into https://www.php.net/manual/en/class.recursivedirectoryiterator.php which is a more powerful method of working with files and directories, although it depends on your use case.

Thanks,
Ash

www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux