AdRock Posted October 11, 2013 Share Posted October 11, 2013 I am pulling some data out of a MySQL database and echoing it out (pretty standard stuff) What is really confusing me is I have checked the query using phpMyAdmin and it returns 1 row which is what i expect. I've even double checked by using a COUNT query and i get the same result. The problem is when I run the returned array through a foreach loop to populate a new array, it appears to create 2 new array rows and i think it's to do with my IF statements. What i want to do in the IF is check is a certain value is present. If not, check that the value is a number and a certain length out of 2 conditions. If not, then do nothing. I'm looking for 3 conditions. Could the problem be that the field type in the database is a varchar and i'm checking for a number? Here is my code $allowed = array('param1', 'param2', 'param3'); //if(isset($_POST['submit'])) { //$sql = 'SELECT consignment_number FROM tracker WHERE order_number = 2893214 AND consignment_number IS NOT NULL'; $sql = 'SELECT column1, column2 FROM table WHERE column1 = :placeholder AND CHAR_LENGTH(column2) > 1'; $stmt = $db->prepare($sql); //$stmt->bindValue('order', $_POST['order']); $stmt->bindValue(':placeholder', $val); $stmt->execute(); $rows = $stmt->fetchAll(); echo count($rows).'<br />'; //echos 1 as checked in phpMyAdmin if(count($rows) != 0) { $myarray = array(); foreach($rows as $row) { $test = $row['column2']; //do something if ($test == 'RM') { // despatched by royal mail $myarray['column1'] = "param1"; $myarray['column2'] = $test; } elseif ( ctype_digit($test) && (strlen($test) == ) { // going by FedEx? $myarray['column1'] = "param2"; $myarray['column2'] = $test; } elseif ( ctype_digit($test) && (strlen($test) == 11) ) { // going by TNT? $myarray['column1'] = "param3"; $myarray['column2'] = $test; } } } else { //no rows } echo count($myarray); echo '<pre>';var_dump($myarray);echo'</pre>'; if(in_array($myarray['column1'], $allowed)) { foreach ($myarray as $array) { if(!empty($myarray['column1'])) { echo $myarray['column1'].': '.$myarray['column2'].'<br />'; } } } else { } Quote Link to comment https://forums.phpfreaks.com/topic/282885-foreach-on-array-adding-extra-rows-to-array/ Share on other sites More sharing options...
Ch0cu3r Posted October 11, 2013 Share Posted October 11, 2013 (edited) The problem is here if(in_array($myarray['column1'], $allowed)) { foreach ($myarray as $array) { if(!empty($myarray['column1'])) { echo $myarray['column1'].': '.$myarray['column2'].'<br />'; } } } The reason you get duplicated results is because the foreach loop is looping through the values (column1 and column2) of $myarray and is running the code within that loop twice! You do not need the foreach loop. The code should be if(in_array($myarray['column1'], $allowed)) { if(!empty($myarray['column1'])) { echo $myarray['column1'].': '.$myarray['column2'].'<br />'; } } The only time you'd need the foreach loop if $myarray contains multiple arrays, eg $myarray = array(); $myarray[0]['column1'] = 'param1'; $myarray[0]['column2'] = '12345678'; $myarray[1]['column1'] = 'bad_param2'; $myarray[1]['column2'] = '87654321'; $myarray[2]['column1'] = 'param3'; $myarray[2]['column2'] = '12345678910'; Then your foreach loop would be foreach ($myarray as $array) { if(in_array($array['column1'], $allowed)) { if(!empty($array['column1'])) { echo $array['column1'].': '.$array['column2'].'<br />'; } } } Then result would be param1: 12345678 param3: 12345678910 Edited October 11, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282885-foreach-on-array-adding-extra-rows-to-array/#findComment-1453526 Share on other sites More sharing options...
Solution AdRock Posted October 11, 2013 Author Solution Share Posted October 11, 2013 Thanks Ch0cu3r . Got it sorted by making the new array as an associative array as sometimes I have more than 1 record returned Quote Link to comment https://forums.phpfreaks.com/topic/282885-foreach-on-array-adding-extra-rows-to-array/#findComment-1453527 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.