Jump to content

finding a just the first key in an array


niche

Recommended Posts

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

<?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

<?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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...