...

View Full Version : Array displays with weird duplicates



alwise17
07-21-2012, 09:32 PM
Hi, i am trying to sort out the arrays for a section of my website, at the moment i am trying to just get them to setup properly before i start using them. The problem i have is that i ouputed them but for some reason they show up as duplicates, is this right? or how do i get rid of the duplicates?

This is what gets displayed on screen


Column name: 0 Column data: default
Column name: ext_token Column data: default
Column name: 1 Column data: default
Column name: ext_token_child Column data: default
Column name: 2 Column data: 4
Column name: concrete Column data: 4
Column name: 3 Column data: 3
Column name: cement Column data: 3
Column name: 4 Column data: 2
Column name: sand Column data: 2
Column name: 15 Column data: 4
Column name: wood_4_2 Column data: 4
Column name: 16 Column data: 2
Column name: wood_8_2 Column data: 2

I want to get rid of all the elements with numbers as the column name

And this is the code


$result = mysql_query("SELECT * FROM extension_material_required WHERE ext_token = 'default' AND ext_token_child = 'default'", $conn2) or die(mysql_error());
$result_array = mysql_fetch_array($result);
$new_result_array = array_filter($result_array, 'strlen');
foreach ($new_result_array as $columnName => $columnData) {
echo 'Column name: ' . $columnName . ' Column data: ' . $columnData . '<br />';
}

Thanks for any and all help

firepages
07-22-2012, 02:02 AM
mysql_fetch_array() returns both numeric and associative indexes so yes the data is effectively doubled instead use


$result_array = mysql_fetch_assoc($result)
//or
$result_array = mysql_fetch_row($result)


mysql_fetch_assoc() is normally easier to work with which will return $result_array['first_fieldname'] rather than $result_array[0] etc



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum