How do you iterate through an associative array in php?

My associative array:

$arr = array[
   1 => "Value1",
   2 => "Value2",
   10 => "Value10"
];

Using the following code, $v is filled with $arr's values

 foreach[$arr as $v]{
    echo[$v];    // Value1, Value2, Value10
 }

How do I get $arr's keys instead?

 foreach[.....]{
    echo[$k];    // 1, 2, 10
 }

Foreever

6,5818 gold badges48 silver badges55 bronze badges

asked Dec 23, 2009 at 9:41

Robin RodricksRobin Rodricks

107k138 gold badges389 silver badges600 bronze badges

2

You can do:

foreach [$arr as $key => $value] {
 echo $key;
}

As described in PHP docs.

answered Dec 23, 2009 at 9:44

codaddictcodaddict

435k80 gold badges487 silver badges524 bronze badges

1

If you use array_keys[], PHP will give you an array filled with just the keys:

$keys = array_keys[$arr];
foreach[$keys as $key] {
    echo[$key];
}

Alternatively, you can do this:

foreach[$arr as $key => $value] {
    echo[$key];
}

answered Dec 23, 2009 at 9:43

Trevor JohnsTrevor Johns

15.5k3 gold badges54 silver badges54 bronze badges

2

Nobody answered with regular for loop? Sometimes I find it more readable and prefer for over foreach
So here it is:

$array = array['key1' => 'value1', 'key2' => 'value2']; 

$keys = array_keys[$array];

for[$i=0; $i < count[$keys]; ++$i] {
    echo $keys[$i] . ' ' . $array[$keys[$i]] . "\n";
}

/*
  prints:
  key1 value1
  key2 value2
*/

answered Oct 19, 2016 at 19:04

5

foreach[$array as $k => $v]

Where $k is the key and $v is the value

Or if you just need the keys use array_keys[]

answered Dec 23, 2009 at 9:44

HtbaaHtbaa

2,31918 silver badges28 bronze badges

I use the following loop to get the key and value from an associative array

foreach [$array as $key => $value]
{
  echo "

$key = $value

"; }

answered Aug 26, 2015 at 15:28

dmeehandmeehan

2,2612 gold badges25 silver badges31 bronze badges

1

The following will allow you to get at both the key and value at the same time.

foreach [$arr as $key => $value]
{
  echo[$key];
}

answered Dec 23, 2009 at 9:44

Jeff BeckJeff Beck

3,9443 gold badges26 silver badges44 bronze badges

While arguably being less clear this method is faster by roughly a factor of roughly 3.5 [At least on the box I used to test]

$foo = array[
    1 => "Value1",
    2 => "Value2",
    10 => "Value10"
];
while[$bar = each[$foo]]{
    echo $bar[0] . " => " . $bar[1];
}

I would imagine that this is due to the fact the foreach copies the entire array before iterating over it.

answered Jul 8, 2014 at 14:49

nettuxnettux

5,0822 gold badges23 silver badges32 bronze badges

Use $key => $val to get the keys:


answered Dec 23, 2009 at 9:46

raphinkraphink

3,5651 gold badge26 silver badges39 bronze badges

0


answered Jun 7, 2017 at 14:47

Oh I found it in the PHP manual.

foreach [$array as $key => $value]{
    statement
}

The current element's key will be assigned to the variable $key on each loop.

answered Dec 23, 2009 at 9:45

Robin RodricksRobin Rodricks

107k138 gold badges389 silver badges600 bronze badges

 foreach[$arr as $key=>$value]{
    echo[$key];    // key
 }

answered Dec 23, 2009 at 9:44

GravitonGraviton

80.2k142 gold badges418 silver badges594 bronze badges

If you use nested foreach[] function, outer array's keys print again and again till inner array values end.


answered Apr 27, 2019 at 8:06

Can we use for loop in associative array in PHP?

So as we can see in the example above, we can easily loop through indexed array using for loop. But for Associative Arrays we need to use ForEach loop.

How can I iterate through an array in PHP?

6 ways to loop through an array in php.
while[expression]{ // Code to be executed }.
do { // Code to be executed } while[expression];.
for [expr1; expr2; expr3] { //Code to be executed }.
array_walk[array|object &$array, callable $callback, mixed $arg = null]: bool..

How do you loop through a multidimensional associative array in PHP?

Answer: Use the PHP nested loop You can simply use the foreach loop in combination with the for loop to access and retrieve all the keys, elements or values inside a multidimensional array in PHP.

What is associative arrays in PHP explain it with simple PHP code?

Associative Array - It refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values. Multiple indices are used to access values in a multidimensional array, which contains one or more arrays.

Chủ Đề