rndilger Posted December 29, 2008 Share Posted December 29, 2008 Hello, I've got an array of mysql field names and I need to create a subset of these array elements (see code below). I would like to extract all of the array elements that begin with 'mem_' as these are the columns in the table that specify information pertaining to members. I presume there is a very easy way to accomplish this, but I'm currently at a loss. Thanks for your help in advance. Ryan $field_names = array(); $cols = mysql_query("SHOW COLUMNS FROM `map`"); for($i=0; $i<mysql_num_rows($cols); $i++){ array_push($field_names,mysql_result($cols, $i)); } Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/ Share on other sites More sharing options...
MadTechie Posted December 29, 2008 Share Posted December 29, 2008 try this <?php $field_names = array(); $cols = mysql_query("SHOW COLUMNS FROM `map`"); while($fields = mysql_fetch_assoc($cols)) { $f = $fields['Field']; if(substr(0,4) == "mem_") { $field_names[] = $f; } } echo"<pre>";print_r($field_names); Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/#findComment-725137 Share on other sites More sharing options...
rndilger Posted December 29, 2008 Author Share Posted December 29, 2008 try this <?php $field_names = array(); $cols = mysql_query("SHOW COLUMNS FROM `map`"); while($fields = mysql_fetch_assoc($cols)) { $f = $fields['Field']; if(substr(0,4) == "mem_") { $field_names[] = $f; } } I like the concept and I believe you're onto something. However, when I run the code, it just outputs 'Array' instead of the name. Ryan echo"<pre>";print_r($field_names); Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/#findComment-725305 Share on other sites More sharing options...
Maq Posted December 29, 2008 Share Posted December 29, 2008 Try: foreach($field_names as $key => $value) echo $value . " - " . $key . " "; Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/#findComment-725335 Share on other sites More sharing options...
rndilger Posted December 29, 2008 Author Share Posted December 29, 2008 Try: foreach($field_names as $key => $value) echo $value . " - " . $key . "<br />"; Sorry, I'm confused as to where I need to add this code or what it needs to replace. I apologize for being such a newbie! Ryan Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/#findComment-725340 Share on other sites More sharing options...
Maq Posted December 29, 2008 Share Posted December 29, 2008 Wherever you have or are trying to display the array: echo"";print_r($field_names); The foreach traverses through the array and grabs the value and key. Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/#findComment-725341 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2008 Share Posted December 29, 2008 Change MadTechie's code to: <?php $field_names = array(); $cols = mysql_query("SHOW COLUMNS FROM `map`"); while($fields = mysql_fetch_assoc($cols)) { if(substr($fields['Field'],0,4) == "mem_") $field_names['Field'] = $fields['Field']; } echo "<pre>". print_r($field_names,true) . '</pre>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/#findComment-725342 Share on other sites More sharing options...
rndilger Posted December 29, 2008 Author Share Posted December 29, 2008 Change MadTechie's code to: <?php $field_names = array(); $cols = mysql_query("SHOW COLUMNS FROM `map`"); while($fields = mysql_fetch_assoc($cols)) { if(substr($fields['Field'],0,4) == "mem_") $field_names['Field'] = $fields['Field']; } echo "<pre>". print_r($field_names,true) . '</pre>'; ?> Ken I seem to be going backwards here. Now the code just outputs: Array ( [Field] => mem_suppliers ) So it's displaying one of the 2 column names in the database (i.e., mem_suppliers). Ryan Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/#findComment-725344 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2008 Share Posted December 29, 2008 Sorry, I wasn't thinking... <?php $field_names = array(); $cols = mysql_query("SHOW COLUMNS FROM `map`"); while($fields = mysql_fetch_assoc($cols)) { if(substr($fields['Field'],0,4) == "mem_") $field_names[] = $fields['Field']; } echo "<pre>". print_r($field_names,true) . '</pre>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/138693-solved-extracting-specific-elements-from-array/#findComment-725361 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.