Jump to content

foreach on array adding extra rows to array


Go to solution Solved by AdRock,

Recommended Posts

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 {




}

 

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 by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.