Search code examples
phparraysimplode

Joining up to 3 values in an array in a loop


I am trying to run some logical tests and matching a list of strings against a database but need help with the array I have.

the Problem

I have an array:

$myArray =
array([0] (
   array [0] ([0] A, [1] B, [2] C, [3] H)
   array [1] ([0] A, [1] D, [2] G, [3] H, [4] L)
   array [2] ([0] D, [1] Z, [2] J, [3] K, [4] O, [5] X)
)
array([1] (
   array [2] ([0] F, [1] Y, [2] W, [3] H, [4] L)
)
array([2] (
   array [0] ([0] O, [1] T, [2] C, [4] O, [5] X)
   array [1] ([0] U, [1] E, [2] E, [3] D)
))

I am trying to test the strings in this array against a list that combines 1, 2 and 3 strings combined with '_' in a table.
Example: A or A_B or A_B_C

Variables and Constants

  • The array length can vary in each of the second dimension of the array
  • Strings can be repeated several times and it has no bearing on the logic
  • The constant however is that the test to combine two strings or three strings is always sequential (e.g. always A_B or A_B_C and not A_D or A_D_J if that makes sense)
  • The sequence moves by one letter each time e.g. first test (A_B_C, A_B and A), then test (B_C_D, B_C and B), then (C_D_E etc...)
  • When the array has insufficient strings to continue testing 2 or 3 combined words, then the test should just run on 1 string

What I am trying to do and desired outcome

I need help with the array syntax to help me build the code into a 3 level logical argument in a loop

something like this:
IF A_B_C exists in myTable do something
ELSE IF A_B exists do something else
ELSE IF A exists do yet another thing
ELSE return blank

I can't quite figure out however how to manipulate the above array so as to arrive at variables

$firstTest = 'A_B_C'; <br>
$secondTest = 'A_B'; <br>
$thirdTest = 'A'; <br>

(I can get thirdTest)

What I tried

through receiving some useful help on other array questions, I tried passing the $myArray through a foreach loop as follow:

foreach ($myArray as $newArray) {
$i = 0;
$j = $i++;

foreach($newArray as $key=>$val) {
    $impArray = array($val[$i],$val[$j]);
    echo implode('_', $impArray);
}
}

However this takes the first string of each first level array.

EDIT:

I have been experimenting with array_slice and do while and can almost get there but the pattern does not quite follow a 3x string combination; instead, it increases from 3 strings to 4 strings, to 5 strings etc... and I do not know why.

Here is my latest attempts:

foreach ($myArray as $newArray => $val) {
    $x = 0;
    $z = 3;
    $route = array();
    
    do  {

        $route = array_slice($val, $x , $z);
        $imp_route = implode('_', $route);
        echo $imp_route;
        $x++;
        $z++;
    } while ( $z <= count($val));
}

Solution

  • Maybe there's a fancier way to do it, but this way makes it clear what you're trying to accomplish:

    foreach ($myArray as $newArray) {
        foreach ($newArray as $valuesArray) {
            for ($i=0; $i<count($valuesArray); $i++) {
                // A_B_C
                if (isset($valuesArray[$i+2])) {
                    echo $valuesArray[$i] . '_' . $valuesArray[$i+1] . '_' . $valuesArray[$i+2];
                }
                // A_B
                if (isset($valuesArray[$i+1])) {
                    echo $valuesArray[$i] . '_' . $valuesArray[$i+1];
                }
                // A
                echo $valuesArray[$i];
            }
        }
    }