phparraysversionssquare-bracket

PHP Version 5.3 vs Version 5.6 and above


to my understanding, PHP version 5.6 and above supports the use of [] (square brackets) for array. I have a functioning code:

$sortedMainCatArray[$letter][] = $eachMainCategory;

that is working well on PHP version 5.6. What this code essentially does it that it pushes array values into $sortedMainCatArray with a preset key. (eg a/b/c) The outcome looks something like this when using print_r.

Array
(
    [A] => Array
        (
          [0] => Array
              (
                [mainCatID] => 33
                [mainCatDesc] => Alternative Medicine
                [mainCatAddedDate] => 2017-12-18 10:35:31
                [mainCatStatus] => active
              )

          [1] => Array
              (
                [mainCatID] => 32
                [mainCatDesc] => Anesthesiology
                [mainCatAddedDate] => 2017-12-18 10:29:48
                [mainCatStatus] => active
              )
        )

    [B] => Array
        (
          [0] => Array
              (
                [mainCatID] => 1
                [mainCatDesc] => Blood & Infection
                [mainCatAddedDate] => 2017-12-18 10:29:28
                [mainCatStatus] => active
              )
        )
)

However, since PHP version 5.3 do not support the use of [] (square brackets), I have to modify my code. What I have was:

$sortedMainCatArray[$letter] = array($eachMainCategory);

However, this only resulted in each alphabetical array to only have one value. An example can be seen below, where under the "A" category, there is only one value (Anesthesiology) instead of two (Alternative Medicine & Anesthesiology).

Array
(
    [A] => Array
        (
          [0] => Array
              (
                [mainCatID] => 32
                [mainCatDesc] => Anesthesiology
                [mainCatAddedDate] => 2017-12-18 10:29:48
                [mainCatStatus] => active
              )
        )

    [B] => Array
        (
          [0] => Array
              (
                [mainCatID] => 1
                [mainCatDesc] => Blood & Infection
                [mainCatAddedDate] => 2017-12-18 10:29:28
                [mainCatStatus] => active
              )
        )
)

How else can I go about to modify my code to work on PHP version 5.3 like it has worked on version 5.6 without the use of [] brackets? Appreciate all the help given! Thank you!


Solution

  • The short array syntax was introduced in 5.4. But you misunderstood. This supports [] instead of array() syntax for literal declarations.

    However, the syntax

    $array[$index][] = $someVar;
    

    has nothing to do with short array syntax. It's been valid since the beginning of PHP. You can use this syntax in older version, too.

    When the code is changed to

    $sortedMainCatArray[$letter] = array($eachMainCategory);
    

    it basically says that do not create a sub-array at $letter index of the main array (multiple calls to this line results in multiple elements in the subarray) but assign array($eachMainCategory) to that index (multiple calls to this line assign the same array to that index multiple times). To sum up, leaving

    $sortedMainCatArray[$letter][] = $eachMainCategory;
    

    for both PHP versions is OK.

    UPDATE

    Accessing an array element at a specific index when that array is returned by a function is called array dereferencing support and was also added in 5.4, so the one liner allowed by it functionThatReturnsArray()[$index] can only be done in two expressions (lines) in PHP prior to 5.4 as shown in the PHP manual examples section:

    // on PHP 5.4 
    $secondElement = getArray()[1];
    
    // previously  
    $tmp = getArray();
    $secondElement = $tmp[1];