awebbdesigner Posted July 12, 2010 Share Posted July 12, 2010 Hi... I have an array '$names' with the following... Array ( [un] => a [pa] => v ) However this array gets used in a for loop and I need to pull the value out using a number i.e. $myname = $names[1] // should print out v When I try this it fails, any tips would be helpful. Or if there is a function for converting array keys into numbers? Quote Link to comment https://forums.phpfreaks.com/topic/207505-array-help/ Share on other sites More sharing options...
JasonLewis Posted July 12, 2010 Share Posted July 12, 2010 You can do a few things, use array_values to return an indexed array with your values, or use a foreach loop instead. Quote Link to comment https://forums.phpfreaks.com/topic/207505-array-help/#findComment-1084870 Share on other sites More sharing options...
bh Posted July 12, 2010 Share Posted July 12, 2010 Hi, you cant index your array with a numeric becouse that is an accociative array. create an enumerative array from your ass.arr example like this (with foreach or use @ProjectFear suggestion [array_values()] which is more beautiful i think): $new_array(); foreach ($names as $name) { $new_array[] = $name; } and after that you can index with a number your enumerative $new_array that holds your $names array content. edit: yeah, i was a bit mindful with the code, sry: $new_array(); foreach ($names as $k => $name) { $new_array[] = $name; } Quote Link to comment https://forums.phpfreaks.com/topic/207505-array-help/#findComment-1084873 Share on other sites More sharing options...
Haraldur1234 Posted July 12, 2010 Share Posted July 12, 2010 That coding is wrong, if you want to array, do like this! <? $string = array ("A", "V"); echo $string[0] echo $string[1] ?> And the result from that will be A V Quote Link to comment https://forums.phpfreaks.com/topic/207505-array-help/#findComment-1084874 Share on other sites More sharing options...
JasonLewis Posted July 12, 2010 Share Posted July 12, 2010 That coding is wrong, if you want to array, do like this! <? $string = array ("A", "V"); echo $string[0] echo $string[1] ?> And the result from that will be A V No, the result from that (assuming short tags are enabled), will be a parse error. While what you have provided is close enough to what can also be done, it depends on how the OP is retrieving the said array. Quote Link to comment https://forums.phpfreaks.com/topic/207505-array-help/#findComment-1084877 Share on other sites More sharing options...
Cagecrawler Posted July 12, 2010 Share Posted July 12, 2010 Example shows three ways to loop through an array, assuming the need for a numeric key is not essential. If you need to pick out a specific key/value pair, you can get a list of keys using array_keys(). $names = array('un' => 'a', 'pa' => 'v'); // Loop using foreach. foreach($names as $k => $v) { var_dump($v); } //Loop using while and list. while(list($key, $val) = each($names)) { var_dump($val); } // Loop using while. while(current($names) !== FALSE) { var_dump(current($names)); next($names); } Quote Link to comment https://forums.phpfreaks.com/topic/207505-array-help/#findComment-1084879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.