This is my code:
foreach($total_columns as $value){
echo "<td><b>{$value}</b></td>";
}
Each $value is being echoed twice, one for the numerical key and one for the associative key. How do I stop this from happening?
This is my code:
foreach($total_columns as $value){
echo "<td><b>{$value}</b></td>";
}
Each $value is being echoed twice, one for the numerical key and one for the associative key. How do I stop this from happening?
I guess your array comes from a mysql record?
If so, then use mysql_fetch_assoc
there!
mysql_fetch_array would output =>
array
(
[0] => "1"
"foo" => "1"
[1] => "2"
"bar" => "2"
)
mysql_fetch_assoc outputs =>
array
(
"foo" => "1"
"bar" => "2"
)
This is probably, where your double entries come from. If so, see the docs here
1) Remove the double keys from the $total_columns in the first place.
2) Check if key is string or integer.
foreach($total_columns as $key => $value){
if(gettype($key) == "integer"){
...
}
}
3) See array_unique from http://php.net/manual/en/function.array-unique.php
Well if you just want to make your code work and don't care about why it doesn't try this:
for($i=0;$i<sizeof($total_columns);$i++)
{
echo "<td><b>{$total_columns[i]}</b></td>";
}
print_r($total_columns);
Result:
[13] => 100 [goods_total] => 100 [14] => 100 [vat_total] => 100 [15] => 200 [gross_total] => 200
It's obvious that you get duplicate values when you try to print them using foreach.
It would be great if you can investigate not to have duplicate values in $total_columns. Still can't understand where do you get them.
let's know the origin of it.
thanks
Or just read the manual for mysql_fetch_array() and actually use the second parameter correctly (default: MYSQL_BOTH / your choice: MYSQL_ASSOC).