How to Loop Through an Associative Array and Get the Key

How to loop through an associative array and get the key?

You can do:

foreach ($arr as $key => $value) {
echo $key;
}

As described in PHP docs.

Loop through associative array of associative arrays using foreach loop

You can make a double foreach to loop all values of all sub arrays.

foreach($students as $key => $value) {
echo 'Key: '.$key.'<br />';
foreach($value as $s_key => $s_value) {
echo 'Sub key: '.$s_key.' => '.$s_value.'<br />';
}
echo '<br />';
}

Result:

Key: rishab
Sub key: age => 25
Sub key: marks => 400
Sub key: class => MCA

Key: kamran
Sub key: age => 23
Sub key: marks => 550
Sub key: class => MBA

Key: Sunil
Sub key: age => 23
Sub key: marks => 550
Sub key: class => MBA

PHP Iterate Through Associative Array and Get Specific Values


foreach ($group_array['user_id'] as $key => $value) {
print($value); // user id
print($group_array['user_first'][$key]);
print($group_array['user_last'][$key]);
}

php - how to loop through values of one specific key in an associative array?

I think this is what you want:

<?php

$arr['Joe'][] = "test1";
$arr['Joe'][] = "test2";
$arr['Joe'][] = "test3";

foreach ($arr['Joe'] as $key => $value) {
echo $value;
}
?>

By adding [] after ['Joe'] the values will be saved like this:

(
[Joe] => Array
(
[0] => test1
[1] => test2
[2] => test3
)

)

How to Loop Array Inside Associative Arrays

Use two foreach loop

<?php 

$siswa = array(
"Kelas-X" => array("Joko", "Budi", "Duduk"),
"Kelas-XI" => array("Entong", "Timun", "Opang"),
"Kelas-XII" => array("Mamat", "Sadaw", "Koreng"),
);

foreach($siswa as $key => $value){
foreach($value as $k => $v){
echo "Key : " . $key. "Value : " . $v;

}
}

?>

PHP mixed associative array how to get values in foreach loop

Each element of an array has a key. "a3" and "a4" aren't keys, they are elements which have numeric keys. You make sure it if you make var_dump of this array

array (size=5)
'a1' =>
array (size=2)
0 => string 'a1b' (length=3)
1 => string 'a1b' (length=3)
'a2' =>
array (size=2)
0 => string 'a2b' (length=3)
1 => string 'a2b' (length=3)
0 => string 'a3' (length=2)
1 => string 'a4' (length=2)
'a5' =>
array (size=2)
0 => string 'a5b' (length=3)
1 => string 'a5b' (length=3)

You can get elements with numeric keys with array_filter function and checking of key type (for example with help is_int function)

$arr = array(
"a1" => array("a1b", "a1b"),
"a2" => array("a2b", "a2b"),
"a3",
"a4",
"a5" => array("a5b", "a5b")
);

$newArr = array_filter($arr, function($key) {
return is_int($key);
}, ARRAY_FILTER_USE_KEY);

or foreach statement:

$arr = array(
"a1" => array("a1b", "a1b"),
"a2" => array("a2b", "a2b"),
"a3",
"a4",
"a5" => array("a5b", "a5b")
);

$newArr = [];
foreach ($arr as $key => $value) {
if (is_int($key)) {
$newArr[] = $value;
}
}

How to iterate over associative arrays in Bash

The keys are accessed using an exclamation point: ${!array[@]}, the values are accessed using ${array[@]}.

You can iterate over the key/value pairs like this:

for i in "${!array[@]}"
do
echo "key : $i"
echo "value: ${array[$i]}"
done

Note the use of quotes around the variable in the for statement (plus the use of @ instead of *). This is necessary in case any keys include spaces.

The confusion in the other answer comes from the fact that your question includes "foo" and "bar" for both the keys and the values.

JavaScript foreach loop on an associative array object

The .length property only tracks properties with numeric indexes (keys). You're using strings for keys.

You can do this:

var arr_jq_TabContents = {}; // no need for an array

arr_jq_TabContents["Main"] = jq_TabContents_Main;
arr_jq_TabContents["Guide"] = jq_TabContents_Guide;
arr_jq_TabContents["Articles"] = jq_TabContents_Articles;
arr_jq_TabContents["Forum"] = jq_TabContents_Forum;

for (var key in arr_jq_TabContents) {
console.log(arr_jq_TabContents[key]);
}

To be safe, it's a good idea in loops like that to make sure that none of the properties are unexpected results of inheritance:

for (var key in arr_jq_TabContents) {
if (arr_jq_TabContents.hasOwnProperty(key))
console.log(arr_jq_TabContents[key]);
}

edit — it's probably a good idea now to note that the Object.keys() function is available on modern browsers and in Node etc. That function returns the "own" keys of an object, as an array:

Object.keys(arr_jq_TabContents).forEach(function(key, index) {
console.log(this[key]);
}, arr_jq_TabContents);

The callback function passed to .forEach() is called with each key and the key's index in the array returned by Object.keys(). It's also passed the array through which the function is iterating, but that array is not really useful to us; we need the original object. That can be accessed directly by name, but (in my opinion) it's a little nicer to pass it explicitly, which is done by passing a second argument to .forEach() — the original object — which will be bound as this inside the callback. (Just saw that this was noted in a comment below.)

Php - Fastest method to loop through associative array keys

If you run a static analysis with a tool like like PHP Mess Detector against that code it will report you have an unused variable.

If all you want are the keys then use array_keys() to get the keys and then loop through them.

foreach (array_keys($arr) as $key) {

Iterate over key/value pairs in associative array in D.

So first, you don't even strictly need a thing:

foreach(key, value; contained) {
// use right here
}

But the .byKeyValue thing can be more efficient, so it is cool to use... just don't specify a type at all (in fact, you very rarely have to with D's foreach, and btw it is always wrong to use auto in the foreach statement - a common mistake since it is used in many other places...)

foreach(item; contained.byKeyValue()) {
writeln(item.key, " ", item.value);
}

Also btw you can use void main if you always want main to return 0 - the language will do that automatically.



Related Topics



Leave a reply



Submit