tags:

views:

44

answers:

2

Hi,

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

A: 

......

$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"];
Sarfraz
-1. You cannot use numeric indexes to access cells with string keys.
nikc
@Felix Kling: Yes you are right, forgot to do that. Fixed anyways.
Sarfraz
@nikc: please see the updated answer. Thanks
Sarfraz
@Sarfraz: I removed the downvote.
nikc
@nikc: Thanks for that :)
Sarfraz
+3  A: 

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).

Felix Kling