1

With a basic associative array like:

$ar = array("First" => 1, "Second" , "Third" =>"Three");

if you do:

foreach($ar as $key => $val) {

  var_dump($key);

}

This will produce:

string 'First' (length=5)

int 0

string 'Third' (length=5)

How do you Achieve the same results in a Multidimensional Array?

Something like:

array( 0 => array(
            "First" => 1, "Two", "Third" => "Three"
                )
     );

I tried:

foreach($ar as $k => $v){

        var_dump($ar[0][$k]);
        }

I got:

string 'Two' (length=3)

Instead of:

string 'First' (length=5)

    int 0

    string 'Third' (length=5)

Thanx

5 Answers 5

3
function get_key(&$arr){
    foreach($arr as $key=>$value){
        if(is_array($value)){
            get_key($value);
        }else{
            var_dump($key);
        }
    }
}
$arr = array(
    'key1'=>array('a'=>1,'b'=>2),
    'key2'=>array('c'=>1,2),
);
get_key($arr);

output:

string(1) "a"
string(1) "b"
string(1) "c"
int(0)
Sign up to request clarification or add additional context in comments.

1 Comment

Cool... But Kinda Overkill. Thanx though
2

If $ar is equal to this:

array( 0 => array(
        "First" => 1, "Two", "Third" => "Three"
            )
 );

You could iterate over the inner array to get the same result:

foreach ($ar as $k => $v) {
    foreach ($v as $k2 => $v2) {
            var_dump($k2);
    }
}

Output:

string(5) "First"
  int(0)
string(5) "Third"

1 Comment

no, it is not perfect, if the question is talking about "multi-dimensional", then it can even be a wrong answer. It just catered 2-layer depth. Nightmare's answer is a way more correct one.
1

You can iterate through the values of the inner array like this:

foreach($ar[0] as $k => $v){
    var_dump($v);
}

The problem with yours is that you are only looping over 1 element in the ar array, and then applying that number to the array inside of it.

If you also want to do the same thing on both levels, you need another foreach loop like so:

foreach($ar as $k => $v){
    foreach($v as $k => $n){
        var_dump($n);
    }
}

2 Comments

Your suggestion Prints even their VALUES... I Want something like: string 'First' (length=5) int 0 string 'Third' (length=5) WITHOUT their VALUES.
What do you mean by without thier values? The only other thing you can get from an array is its key and you just change $v to $k in that case.
1
$arr = array(
    0 => array("First" => 1, "Two", "Third" => "Three")
);

foreach ($arr as $key => $value)
{
    foreach ($value as $k => $v)
    {
        var_dump($k);
    }
}

Comments

1

You can access multidimensional Array keys and indexes like so:

$ary = array('whatever' => array('First' => 1, 'Two', 'Third' => 'Three'),
  array('something' => array('cool' => 'yes', 'awesome' => 'super', 'great' => 10)
);
foreach($ary as $k => $a){
  // $k is each Array key within initial Array
  foreach($a as $i => $v){
    // $i is internal index
    // $v is internal value
  }
}

Note:

In an Array that contains Arrays the first Array is really the value under key 0, unless the Array is assigned to a key, so your key, 0, is not necessary.

1 Comment

Cool... though it can make a Beginner NOSE-BLEED!... llz

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.