Hi there. I haven't done much programming with PHP since 5.6. I notice a web app I designed no longer works because i had the following code:

	if($anPregResult){
		foreach($alnumParam as $anFuncKey => $anFuncVal){
			if($firstChar === $anFuncKey) { 
				$alnumParam[$anFuncKey][] = $thisDirObject; 
				break 1;
			}
		}
	}

How do we handle that $array[$key][] = whatever sort of assignment now? Xdebug tells me it's invalid in PHP 7.4
I tried to fix it like this, but it seems unnecessarily clunky, and... it doesn't work as the prev code did

	if ($anStrposResult !== false) {
		$addAlphaNumCounter = 0;
		foreach ($alnumParam as $anFuncKey => $anFuncVal) {
			if(ucfirst($firstChar) === ucfirst($anFuncKey)) {
				/* $alnumParam[ucfirst($anFuncKey)][] = $thisDirObject; 
				break 1; */
				$alnumParam[ucfirst($anFuncKey)][$addAlphaNumCounter] = $thisDirObject; 
				$addAlphaNumCounter++;
				break 1;
			}
		}
	}

Thanks for reading!

BTW
I had an account here, many years ago, but the userid / pass doesn't work anymore. i realize the forum has been upgraded since then. 🙁
i don't know if the old stuff still exists or not, but I must admit I'm curious what I posted here in the past. I recall it being my preferred PHP resource for help. 🙂

    It looks like your initial problem is that the values of $alnumParam are strings, not arrays. Therefore you cannot append array elements to them, since they themselves are not array. Not knowing what the actual intent of all this is, it's hard to suggest a "correct" solution. I got this to "work" without errors, but no idea if it would be of any use for what you are trying to do:

    <?php
    $anPregResult = true;
    $alnumParam = [
        'foo' => [],  // initializing these as empty arrays
        'bar' => []
    ];
    $firstChar = 'bar';
    $thisDirObject = "some object?";
    
    if ($anPregResult) {
        foreach ($alnumParam as $anFuncKey => $anFuncVal) {
            if ($firstChar === $anFuncKey) {
                $alnumParam[$anFuncKey][] = $thisDirObject;
                break 1;
            }
        }
    }
    
    var_export($alnumParam);
    

    Output:

    array (
      'foo' => 
      array (
      ),
      'bar' => 
      array (
        0 => 'some object?',
      ),
    )
    

    Thanks NogDog

    I have to re-examine this code. Basically what it's supposed to do is look at files in a given directory and create HTML so the files can be viewed in the browser. the $anFuncKey var is an alphanumeric string, for example "1", or "A" corresponding to files name "123.html", "A_style_file__.css", "Another_static_file.png", etc.

    So, a <ul><li> [ A ]element can be assigned the value of $anFuncKey, then a new sub-list containing the <ul><li><a href="A_style_file.css"> $filename </a></li> <li> <a href="Another_static_file.png">
    and so on. I have jQuery set up to expand and collapse the lists. (probably a waste of time, but it was a project/ something to do).

    I have to see why I was trying to make that a multidimensional array. It's not making sense at the moment.

    ajaxStardust It's not making sense at the moment.

    It's almost always easier to write the code once you really figure out what is needed. 😉

      17 days later

      I want to rewrite it. What I want it to do (and how it used to work):

      • Look at the files in a given directory and gather data about them
      • the data will be used to create an alphanumeric list of those objects
      • use that alphanumeric data result to display info about those objects in such a way as to view the filenames as HTML output, and perhaps click the file itself to view it in the browser if it is HTML, txt, .conf, etc.

      I already have the HTML / .js part setup in a generic way to suit me for now. I need a component that will iterate over the filesystem. I was trying to put the info as described above into a multidimensional array so i could have $array[$sortCharAsKey][$objectAsStringName]

      Let a directory contain the following objects:
      A_file.txt
      B_file.ini
      B2_file.jpg
      directory
      disk_db.sql
      sname.html
      sname.js

      // $anKey will be assigned value of the first char of the filename, in order to match the array key / values for the $alphaNum[$forEachKey] array in a foreach loop using something based on

      while (($thisObject = readdir($dh)) !== false) { ... 
      
      $alaphaNum[$anKey]  // where $anKey in this instance might be D to match the objs found: 
      // "directory", and "disk_db.sql". , 

      $alphaNum is an array whose values were created like so:
      (sorry for semantic errors... i'm trying to just provide the example without pasting a bunch of code)

      $alphaNumString = '123abcd_etc';
      $alphaNum = str_split($alphaNumString); 
      $alphaNum = array_flip($alphaNum);

      it's not actually the same variable assignment at this point, but for illustration of concept as to why i'm trying to assign values to an array like that. so...

      echo $alaphaNum[6][0] ."\n". $alphaNum[6][1]; // assuming $alphaNum[6] corresponds to D
      // would print :

      directory
      disk_db.sql

      I used to be able to accomplish it by assigning the value to and creating the multidimensional array using the empty brackets, like so:

      $alphaNum[$anKey][] = $objectString 
      // where $objectString is "<html stuff>some_file_.txt</html stuff>"

      but newer versions of PHP don't support it, and I don't understand how to do what i'm trying to do otherwise. So i'm ready to look at someone else's code at this point. 🙂

        $alphaNum = str_split($alphaNumString); 
        $alphaNum = array_fill_keys($alphaNum, []);

        So that $alphaNum[$anKey] is already an array when you start trying to append elements to it.

        $alphaNum[$anKey][] = $objectString;

        Previously it would have been an integer (what used to be the key of $alphaNum before you flipped it), and integers aren't arrays. As of PHP 8.0, attempting to append elements to an integer will cause an error to be thrown. In previous versions a warning would have been issued (and the attempt would have been ignored).

        2 months later

        Weedpacket

        Thank you so much for this reply! It fixed my problem, and helped me to recognize another problem (which I actually look forward to fixing, unlike this which had me pulling my hair out!)!

        BTW, i remember you from this forum circa 2006 - 2007 or so. I recall you being very helpful! in fact, likely an inspiration to keep going when I thought I might never get it. That's important.
        i couldn't figure out how to recover my old userId... thought maybe it wasn't the same forum anymore, so i just created a new acct.
        (any advise on that would be helpful as well... )

        I VERY MUCH look forward to working with the code... now that you've taught me how to use that function correctly.

        Thank you! I'm quite pleased to be able to move forward with my silly little project. 😀

        In my post, Dec 11, 2021, you'll see a string variable assignment, i use that string to create an array.
        $alphaNumString = '123abcd_etc';

        the idea is to sort a list of files alphabetically . it would be much more useful if that string were built dynamically. ideally, to scan a directory, and gather the first character of the objects in that directory.

        e.g. file list: amber.txt, apple.htm, bubble.php, cat.txt, horse.txt, truck.png

        would result in a string acquired dynamically "ABCHT" assigned to a variable. i could then use that dynamically created string to have a more accurate listing in my alphanumeric list.

        i'm just grabbing the first character of the filename because i'm sorting them into collapsible sub lists in php/html elsewhere.

        what might be a way to approach building a simple string like that, while avoiding duplicate characters? in other words, in the example above-- while there are 2 "a" files, i only need to gather 1 a into my alphanumeric string to build my html for both files.

        ajaxStardust what might be a way to approach building a simple string like that, while avoiding duplicate characters?

        One possibility (TMTOWTDI):

        $files = ['abcd', 'efgh', 'afch', 'xyz', 'egbdf'];
        $inits = [];
        foreach($files as $file) {
          $inits[substr($file, 0, 1)] = 1;
        }
        $inits = array_keys($inits);
        var_export($inits);
        
        /*
        output:
        
        array (
          0 => 'a',
          1 => 'e',
          2 => 'x',
        )
        */
        
        $letters = array_unique(array_map(fn($fname) => $fname[0], glob('*.*')));
        

        Fortunately, glob sorts its results by default. If you want $letters to be a string instead of an array (an array sounds more useful) then $letters = join(...)

        Write a Reply...