1

how can i convert an index to an string ?

For example i would like to get the 'signin' index here:

array(1) {
  ["signin"]=>
  array(2) {
    ["email_address"]=>
    string(0) ""
    ["password"]=>
    string(0) ""
  }
}

Javi

1
  • Does this need to be for "indexes" meaning offset/position: i.e., "tell me the key for the nth array item"? Commented Apr 20, 2010 at 11:23

2 Answers 2

3

You can use key():

reset($array); // resets the internal pointer to the first element, 
               // might not be necessary
$current_key = key($array);
// $current_key = 'singin';

Use array_keys() to get all keys of an array, e.g.:

$keys = array_keys('foo'=>1, 'bar'=>2);
// $keys[0] = 'foo'
// $keys[1] = 'bar'

To make sure that the key is a string you can use strval() (in case you also have numerical indecies).

Sign up to request clarification or add additional context in comments.

Comments

0

......

$signin = $your_array["signin"];

To get the key, you can use the key

$key = key($your_array);

More info here: http://php.net/manual/en/function.key.php

The $signin itself is an array, you can check out:

print_r($signin);

So you can also get email address and password like this if you want:

$email = $your_array["signin"]["email_address"];
$password = $your_array["signin"]["password"];

2 Comments

-1. You cannot use numeric indexes to access cells with string keys.
@Felix Kling: Yes you are right, forgot to do that. Fixed anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.