niche Posted October 11, 2010 Share Posted October 11, 2010 foreach($_POST as $key => $value) { $up = $key; echo $up; } will echo all the keys.How do I change this script to echo only the first key? Thanks Link to comment Share on other sites More sharing options...
jeffman Posted October 11, 2010 Share Posted October 11, 2010 You're on the right track. Look at the each function. array_keys might also simplify the task.I am not sure if the sequence of post array elements is guaranteed by rule, or unwritten agreement, or nothing at all. Someone might want to chime in on that. Link to comment Share on other sites More sharing options...
niche Posted October 11, 2010 Author Share Posted October 11, 2010 <?php$foo = array("LAMS_ZV9014" => "25.00" , "ABC" => "10.00" );$bar = each($foo);print_r($bar);?>produces: Array ( [1] => 25.00 [value] => 25.00 [0] => LAMS_ZV9014 [key] => LAMS_ZV9014 ) so far, i'm chasing my tail trying to get the key from $bar into a local variable. I'm so close, but yet so far.ideas? Link to comment Share on other sites More sharing options...
ShadowMage Posted October 11, 2010 Share Posted October 11, 2010 <?php$foo = array("LAMS_ZV9014" => "25.00" , "ABC" => "10.00" );$bar = each($foo);print_r($bar);?>produces: Array ( [1] => 25.00 [value] => 25.00 [0] => LAMS_ZV9014 [key] => LAMS_ZV9014 ) so far, i'm chasing my tail trying to get the key from $bar into a local variable. I'm so close, but yet so far.ideas?Array ( [1] => 25.00 [value] => 25.00 [0] => LAMS_ZV9014 [key] => LAMS_ZV9014 )echo $bar['key']; Link to comment Share on other sites More sharing options...
jeffman Posted October 11, 2010 Share Posted October 11, 2010 $bar[0] or $bar['key'] returns what you want. If you call each() again, it will return the second pair, but the first time gets you the first pair. Link to comment Share on other sites More sharing options...
niche Posted October 11, 2010 Author Share Posted October 11, 2010 If that had been a snake, it would have bit me. Fortunately, Deirdre's Dad and ShadowMage are faster!Thanks,Niche Link to comment Share on other sites More sharing options...
boen_robot Posted October 12, 2010 Share Posted October 12, 2010 It's a good think you aren't dealing with a Python (script) then :)BTW, you could also break unconditinally at the first iteration, like: foreach($_POST as $key => $value) { $up = $key; echo $up; break;} But that particular approach is best only when you have some conditions within the loop... like, if you wanted the first N keys for example. Link to comment Share on other sites More sharing options...
niche Posted October 12, 2010 Author Share Posted October 12, 2010 Nice touch. Thanks boen_robot. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.