0

I have a query the returns columns from the INFORMATION_SCHEMA. I then create an array, use the array in a foreach loop to create another array.

The issue I have is the final array starts at a position 0, producing column SeqID0 but the first column is SeqID1:

Array ( [0] => SeqID0 [1] => SeqID1 [2] => SeqID2 [3] => SeqID3 [4] => SeqID4 [5] => SeqID5 [6] => SeqID6 [7] => SeqID7 [8] => SeqID8 [9] => SeqID9 [10] => SeqID10 [11] => SeqID11 [12] => SeqID12 [13] => SeqID13 [14] => SeqID14 [15] => SeqID15 [16] => SeqID16 [17] => SeqID17 [18] => SeqID18 [19] => SeqID19 [20] => SeqID20 [21] => SeqID21 [22] => SeqID22 [23] => SeqID23 [24] => SeqID24 [25] => SeqID25 [26] => SeqID26 [27] => SeqID27 [28] => SeqID28 [29] => SeqID29 [30] => SeqID30 [31] => SeqID31 [32] => SeqID32 [33] => SeqID33 [34] => SeqID34 [35] => SeqID35 [36] => SeqID36 [37] => SeqID37 [38] => SeqID38 [39] => SeqID39 [40] => SeqID40 [41] => SeqID41 [42] => SeqID42 [43] => SeqID43 [44] => SeqID44 [45] => SeqID45 [46] => SeqID46 ) 

How can I make the array start at SeqID1.

This is my code;

$strSQLCol = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_NAME= 'NXLHR_Active' AND COLUMN_NAME LIKE 'SeqID%'";

$rsCol = mysqli_query($link,$strSQLCol);                                        

$SeqList = array();

do {
    $SeqList[] = $row;
} while($row = $rsCol->fetch_assoc());

$SeqIDArray = array();
foreach($SeqList as $key => $value) {
    $SeqIDArray[] = 'SeqID' . $key;
}

Many thanks in advance for your time.

1 Answer 1

1

Here your improved code. Notice I have added a conditional in foreach:

foreach($SeqList as $key => $value) {
    if (1 > $key) { continue; }
    $SeqIDArray[] = 'SeqID' . $key;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @rokas, thanks for your reply which did work, but for whatever reason the "foreach" is not returning the last column. The array result looks like: Array([0]=>SeqID1=>SeqID2 - and so on until - SeqID45=>SeqID46, but is missing SeqID47. Any ideas why this would be. If I do "print_r ($SeqList);" and echo the result of the INFORMATION_SCHEMA its returns all upto and including SeqID47.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.