0

consider the following array

$arr1=array('a'=>2,'b'=>22,'c'=>4,'d'=>10)

Now,for

print_r(array_keys($arr1));

output is

Array ( [0] => a [1] => b [2] => c [3] => d )  

Fine because return type of array_keys is array. Even echo array_keys($arr1); gives Array as output.

But,for

foreach(array_keys($arr1) as $key)
 {
     echo $key.'<br/>';
  }

output is abcd Can someone explain how foreach is working here as i was expecting the following output

[a] => 2 
[b] => 22 
[c] => 4 
[d] => 10 

Do not want a better code so as to display my expected output.Just want to know how foreach is looping in this case.

Mayur
  • 291
  • 1
  • 3
  • 16
  • Why should the `foreach` lead to this output when all you have in your `echo` is `$key` and a HTML line break? You can get key **and** value in a loop with `foreach ($array as $key => $value)` and then output both. Might be a little bit too much, but maybe have a look at [this question](http://stackoverflow.com/questions/10057671/how-foreach-actually-works?rq=1). – Till Helge Apr 24 '13 at 11:18

5 Answers5

2

array_keys($arr1) gets the array keys so it's looping throught ["a","b","c","d"].

furthermore your output for print_r(array_keys($arr1)) is wrong since it outputs :

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

not what you have written

i quote you :

Now,for

print_r(array_keys($arr1)); output is

Array ( [a] => 2 [b] => 22 [c] => 4 [d] => 10 )

which is not correct

mpm
  • 20,148
  • 7
  • 50
  • 55
  • oops...copy-paste mistake...!! – Mayur Apr 24 '13 at 12:04
  • `array_keys($arr1)` does get it looping through a,b,c,d and assign them to $ key through subsequent loops but sud it not assign [0]=>'a',[1]=>'b',[2]=>'c' and [3]=>'d' since the output of array_keys is array – Mayur Apr 24 '13 at 12:09
  • all arrays are "assiociative" in PHP , wether the index is a integer or a string. – mpm Apr 24 '13 at 12:26
0

You can use this syntax :

<?php
foreach($arr1 as $key => $values) {
        echo $key . "=>" . $values;
    }
?>
AFrenchDev
  • 151
  • 7
  • "HOW the output came" is the issue here,rather than "what sud be the code to get the output"..anyways,thanks..!! – Mayur Apr 24 '13 at 12:12
0

foreach(array_keys($arr1) as $key) iterates over the keys of $arr1, assigning the current key to $key in turn. 'current' means the key of the current iteration.

collapsar
  • 17,010
  • 4
  • 35
  • 61
0

Because that's simply how foreach works. Since array_keys returns the keys ("a","b",...) of your array array_keys($arr1) is the same as Array("a","b","c","d"), which is

Array( [0] => "a", [1] => "b", ... )

Since you want to iterate over both keys and values in your array you should simply use

foreach($arr1 as $key => $value){
    echo "[{$key}] = {$value}<br/>";
}
Zeta
  • 103,620
  • 13
  • 194
  • 236
0

Function array_keys returns all the keys of your array. To get what you wanted, you should use something like this

foreach($arr1 as $key => $value) {
    echo $key . "=>" . $value;
}
ljubiccica
  • 480
  • 3
  • 17