PHP Loop Through Associative Arrays

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.

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;

}
}

?>

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

Loop through associative array and assign values

$instructions = array (
array("step_no"=>"1","description"=>"Ensure that you have sufficient balance"),
array("step_no"=>"2","description"=>"Approve the request sent to your phone")
);

foreach($instructions as $instruction) {
echo $instruction['step_no'] . '. ' . $instruction['description'] . "\n";
}

If it's HTML you may want to use <ol> and <li>.

Iterating over a complex Associative Array in PHP

Nest two foreach loops:

foreach ($array as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print " $key => $value\n";
}
print "}\n";
}

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;
}
}

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]);
}

How to loop through json object, which has been converted to associative array, to get prop value

$element will refer to the values inside already.

forEach($assArr["features"] as $element){
if ($element["properties"]["iso_a2"] == "BS"){
echo_r($element);
}
}

If you need the 0, you can do the following

forEach($assArr["features"] as $key => $element){
if ($element["properties"]["iso_a2"] == "BS"){
echo_r($key); // will be 0
echo_r($element);
}
}


Related Topics



Leave a reply



Submit