sKunKbad Posted November 7, 2009 Share Posted November 7, 2009 If I have an array with only one key => value pair, like this: Array ( [black] => 5 ) I know I can use foreach to get the key and value, but since there is only one key=>value pair, and there will only ever be one key=>value pair, I was thinking there has to be a better way than using foreach. At first I thought that I should use [0], but that results in an undefined index. Should I just use foreach, or is there a better way to get both key and value. Quote Link to comment https://forums.phpfreaks.com/topic/180684-solved-getting-a-single-array-key-name-and-value-name-when-the-key-is-not-known/ Share on other sites More sharing options...
Andy-H Posted November 7, 2009 Share Posted November 7, 2009 $arr = array('black' => 5); $key = array_keys($arr); $key = $key[0]; $val = $arr[$key]; echo 'Output: ' . $key . ' => ' . $val; ########## ##Output## ########## Output: black => 5 Quote Link to comment https://forums.phpfreaks.com/topic/180684-solved-getting-a-single-array-key-name-and-value-name-when-the-key-is-not-known/#findComment-953302 Share on other sites More sharing options...
Alex Posted November 7, 2009 Share Posted November 7, 2009 You can just use current() and array_search <?php $array = Array( 'something' => 'value' ); $val = current($array); $key = array_search($val, $array); echo "$key => $val"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/180684-solved-getting-a-single-array-key-name-and-value-name-when-the-key-is-not-known/#findComment-953306 Share on other sites More sharing options...
sKunKbad Posted November 7, 2009 Author Share Posted November 7, 2009 I had tried using array_keys, but for some reason even after a second attempt, it still wouldn't work. Using current() , which is something I've never used, worked great! What kind of complicates things is that this array is inside an array, but it does work now: <?php // redirect if only one option if (count($rates_array) == 1) { $ak = array_keys( $rates_array ); if( count( $rates_array[ $ak[0] ] ) == 1) { $ak2 = $rates_array[ $ak[0] ]; $_SESSION['checkout_data']['shipping_fee'] = current($ak2); $_SESSION['checkout_data']['shipping_type'] = array_search( current($ak2) , $ak2); header("Location: ".secure_base_url()."checkout/card_accept/one_ship".SITE_FILE_EXTENSION, TRUE, 302); } } Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/180684-solved-getting-a-single-array-key-name-and-value-name-when-the-key-is-not-known/#findComment-953312 Share on other sites More sharing options...
salathe Posted November 7, 2009 Share Posted November 7, 2009 Maybe I'm missing something obvious but you seem to be doing more work than might be necessary. <?php // redirect if only one option if (count($rates_array) == 1) { $rate = current($rates_array); if (count($rate) === 1) { $_SESSION['checkout_data']['shipping_fee'] = current($rate); $_SESSION['checkout_data']['shipping_type'] = key($rate); header("Location: ".secure_base_url()."checkout/card_accept/one_ship".SITE_FILE_EXTENSION, TRUE, 302); } } Quote Link to comment https://forums.phpfreaks.com/topic/180684-solved-getting-a-single-array-key-name-and-value-name-when-the-key-is-not-known/#findComment-953343 Share on other sites More sharing options...
sKunKbad Posted November 8, 2009 Author Share Posted November 8, 2009 Maybe I'm missing something obvious but you seem to be doing more work than might be necessary. The one and only Salathe I presume? I believe you have helped me, or been involved in discussions with me on Sitepoint and Kohana forums. Yes, you are obviously correct. I was doing too much work. Oddly enough, in the 3.5 years I've been using php, I've never used current() or key(), so these functions are new to me. While I'm glad that I was able to solve my problem, I'm happy to have had my problem so I could learn something. Quote Link to comment https://forums.phpfreaks.com/topic/180684-solved-getting-a-single-array-key-name-and-value-name-when-the-key-is-not-known/#findComment-953550 Share on other sites More sharing options...
salathe Posted November 8, 2009 Share Posted November 8, 2009 The one and only Salathe I presume? I believe you have helped me, or been involved in discussions with me on Sitepoint and Kohana forums. One and the same, I get around a bit. Quote Link to comment https://forums.phpfreaks.com/topic/180684-solved-getting-a-single-array-key-name-and-value-name-when-the-key-is-not-known/#findComment-953598 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.