jeudi 23 juin 2016

Combining certain strings in an array based on positive matches against mySQL table

I have a multidimensional array composed of strings of various length as follow: $a = Array ( [0] Array ( [0] AQWER, [1] CFG, [2] JUHTYREWQ, [3] K, [4] LO ), Array [1] Array ( [0] VFG, [1] yhtredw, [2] koeutydjwiq, [3] bg, [4] hytefs, [5] M), Array [2] Array ( [0] BHTWQ, [1] BH, [2] NJUy)) Equally, I have a mySQL table containing the following values myTable AQWER CFG JUHTYREWQ_K_LO VFG yhtredw bg_hytefs BHTWQ BH_ NJUy Desired outcome I am trying to test and reformulate the array based on matches in the SQL table so that it looks like this: $a = Array ( [0] Array ( [0] AQWER, [1] CFG, [2] JUHTYREWQ_K_LO ), Array [1] Array ( [0] VFG, [1] yhtredw, [2] koeutydjwiq, [3] bg_hytefs, [5] M), Array [2] Array ( [0] BHTWQ, [1] BH_NJUy)) And to still show values that have not been found (e.g. koeutydjwiq) What I have tried so far: I received great help in helping me manipulate $a so as to be able to test 1 string, 2 strings and 3 strings combinations. However my code does not successfully pick up string combinations (that are definitely there in both the Array and the Table) and as a result, does not reformat the original array $a and I can't quite figure out why. In fact more specifically, when calling the print_r($para) in the IF statements, I get ......... Here is my code: foreach ($a as $val) { for ($i=0; $i<count($val); $i++) { // A_B_C if (isset($val[$i+2])) { $exagon = array(); $exagon = $val[$i] . '_' . $val[$i+1] . '_' . $val[$i+2]; $conn = mysqli_connect("localhost:8889","root","root","myDB"); $query = "SELECT * FROM `myTable` WHERE LIST = '".$exagon."'"; $para = array( ); $result = mysqli_query($conn, $query); //echo $result; while($row = mysqli_fetch_array($result)) { $para[] = array($row['LIST']); } if (isset($para) && !empty($para)) { print_r($para); array_splice($a, $i, $i+2, $para); $i=$i+2; } else { unset($para); unset($exagon); } } // A_B elseif (isset($val[$i+1])) { $exagon = array(); $exagon = $val[$i] . '_' . $val[$i+1]; $conn = mysqli_connect("localhost:8889","root","root","myDB"); $query = "SELECT * FROM `myTable` WHERE LIST = '".$exagon."'"; $para = array( ); $result = mysqli_query($conn, $query); while($row = mysqli_fetch_array($result)) { $para[] = array($row['LIST']); } if (isset($para) && !empty($para)) { print_r($para); array_splice($a, $i, $i+1, $para); $i=$i+1; } else { unset($para); } } // A else { echo $val[$i]; } } } Admittedly, through research, I have found posts and manuals to guide me on using array_splice and calling out variables in a SQL query but it is quite possible multiple errors are present in this code

Aucun commentaire:

Enregistrer un commentaire