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.
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
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)
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));
}
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];
}
}
}