0

I have a multidimensional array and would like to call the key and the value separately, of individual levels inside the array.

Now with a normal array, I can do this by writing:

$example = array('one', 'two', 'three');
echo $example[0]; 

and this will return: one

However, when I try to do this with a multidimensional array like so:

$example = array(
'optionone' => array('one', 'two', 'three'),
'optiontwo' => array('a','b','c'),
'optionthree' => array(1,2,3)
);
echo $example[0];
echo $example[0][0];

Instead of echoing 'optionone' followed by 'one' I get nothing returned, and no error code.

When trying to see why this is happening by using:

var_dump($example[0)];

or

var_dump($example[0][0]);

NULL is returned.

I would really appreciate if someone could tell me how to correctly get/echo/call 'optionone' to return from the array as a string: 'optionone' without writing the name and how can I call the value 'one' without writing:

$example['optionone'][0];

I'm trying to create something where it doesn't know the key name, so it rather is going by the position of the keys but i would like to be able to return the key names at different points in the program but I can't seem to figure out how to do this.

1
  • array_keys function Commented Jun 25, 2015 at 18:06

3 Answers 3

4

This should work for you:

Just get your associative keys into an array with array_keys(), so you can access them as a 0-based indexed array, e.g.

So with array_keys($example) you end up with the following array:

Array
(
    [0] => optionone
    [1] => optiontwo
    [2] => optionthree
)

Which you then can access as you want it:

<?php

    $example = array(
        'optionone' => array('one', 'two', 'three'),
        'optiontwo' => array('a','b','c'),
        'optionthree' => array(1,2,3)
    );

    $keys = array_keys($example);
    echo $keys[0] . "<br>";
    echo $example[$keys[0]][0];

?>

output:

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

Comments

1

The outer array in your example is an associative array. There is not value at index 0. If you need to get to to the first key in the associative array without knowing the name of the key you would need to do something like:

$example = array(
'optionone' => array('one', 'two', 'three'),
'optiontwo' => array('a','b','c'),
'optionthree' => array(1,2,3)
);
//get array keys into array
$example_keys = array_keys($example);
// get value at first key in $example array
$first_inner_array = $example[$example_keys[0]];
// get first value from first inner array
$first_value = $first_inner_array[0];


// Or to get an arbitrary value:
$x = ... // whatever inner array you want to get into
$y = ... // whatever index within inner array you want to get to
$value = $example[$example+keys[$x]][$y];

I would say that if you find yourself doing this in code, you probably have your data structured poorly. Associative arrays are not meant to convey any sort of element ordering. For example, if you changed your example array to this:

$example = array(
'optiontwo' => array('a','b','c'),
'optionone' => array('one', 'two', 'three'),
'optionthree' => array(1,2,3)
);

You would get a different value retrieved from above code even though the relationship between outer array keys and inner array contents has not been changed.

Comments

0

not tested

 foreach($example as $f_array){
    foreach($f_array as $key => $val){ //iterate over each array inside first array
        echo $key; //should print 'optionone'          
        echo $val[0]; //should print 'one'
        break;
    }
 }

Comments

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.