Jump to content

[SOLVED] Getting a single array key name and value name when the key is not known


Recommended Posts

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.

$arr = array('black' => 5);
$key = array_keys($arr);
$key = $key[0];
$val = $arr[$key];

echo 'Output: ' . $key . ' => ' . $val;

 

##########
##Output##
##########
Output: black => 5

You can just use current() and array_search

<?php
$array = Array(
'something' => 'value'
);
$val = current($array);
$key = array_search($val, $array);

echo "$key => $val";
?>

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!

Maybe I'm missing something obvious but you seem to be doing more work than might be necessary.  :shrug:

 

<?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);
}
}

Maybe I'm missing something obvious but you seem to be doing more work than might be necessary.  :shrug:

 

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.

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. :shy:

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.