tHud Posted January 27, 2017 Share Posted January 27, 2017 Hello everyone I have an array like this... Array ( [0] => A27 [1] => A25+A26 [2] => B1 [3] => B7 [4] => C10 [5] => A3 [6] => B5 [7] => C1+C2 [8] => A1 [9] => A6 [10] => C8 [11] => A12 [12] => C14 [13] => A15 [14] => C11 [15] => B3+B4 ) I would like to remove the values after the + sign and replace the second part back in the array. Something like this... Array ( [0] => A27 [1] => A25 [2] => B1 [3] => B7 [4] => C10 [5] => A3 [6] => B5 [7] => C1 [8] => A1 [9] => A6 [10] => C8 [11] => A12 [12] => C14 [13] => A15 [14] => C11 [15] => B3 [16] => A26 [17] => C2 [18] => B4 ) I think I've been away from PHP too long because I've tried all sorts of ways of doing this and I am really stuck. Thank you for any suggestions. Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 27, 2017 Share Posted January 27, 2017 (edited) Where is the array coming from? A Database? How is the data getting like that in the first place? Edited January 27, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
tHud Posted January 27, 2017 Author Share Posted January 27, 2017 I'm sorry, this is the code. $query = "SELECT assignTable FROM EXHIBITORS WHERE year = $newYear"; $result = $mysqli->query($query) or die("There was a problem with the query: " . mysqli_error($mysqli)); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) {$myResults[] = $row; } } $myVals = array_column($myResults, 'assignTable'); echo '<pre>'; print_r($myVals); echo '</pre>'; I've been asked to alter some code of a guy that left the company. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 27, 2017 Share Posted January 27, 2017 (edited) So the problem exists because of a poor database design. Combining values in a single column is not the way to store data. Anyway try this: if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { // break up the values at the + sign $myResults = explode('+', $row['assignTable']); foreach($myResults as $d) $myVals[] = trim($d); } } Edited January 27, 2017 by ginerjm Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 27, 2017 Share Posted January 27, 2017 (edited) Your code is vulnerable to an SQL Injection Attack. You never ever put variables in a query. You need to use prepared statements. It is also a security problem outputting DB errors to the user. That info is only good to a programmer or a hacker. I suspect you have many other problems as well. Edited January 27, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
tHud Posted January 27, 2017 Author Share Posted January 27, 2017 So the problem exists because of a poor database design. Combining values in a single column is not the way to store data. Anyway try this: Oh thank you so much ginerjm, I'm always overthinking things! I really appreciate your help. @benanamen - Thank you for your input. I am aware of this, it's not my code. I only saw it today for the first time. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 27, 2017 Share Posted January 27, 2017 HTH! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.