This question already has answers here:
Loop through associative array of associative arrays using foreach loop [closed]
(2 answers)
Closed 1 year ago.
I create an associative array in which there is an array, I want to print an associative array (key) and an array that is in it (value)
I have tried using a foreach but only managed to print the key but it shows an error for its value (Error: Array To String Conversion).
The second experiment, I tried using the foreach loop for the key and then used the loop for to print the value (Error: Undefined Offset).
<?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){
echo "Key : " . $key . "Value : " . $value;
}
?>
You cannot use echo on an array, you have to convert it to string before.
You can use json_encode for that.
Like this :
echo "Key : " . $key . "Value : " . json_encode($value);
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;
}
}
?>
Related
This question already has answers here:
How To Access Values In Associative Array Using PHP
(3 answers)
Closed 3 years ago.
How can I access the values that the associative array brings in "version" separately? I mean, I want to be able to bring v1, v2 and v3 separately to use them.
<?php
$version = array("v1", "v2", "v3");
$newarray = array("value1" => "Value 1",
"value2" => "Value 2",
"version" => implode($version));
I understand that with a foreach it is possible, but I do not know how. And on the other hand, I do not know if it's the best way to assign that array "version" with implode
Implode has no use in this example.
<?php
$version = array("v1", "v2", "v3");
$newarray = array("value1" => "Value 1",
"value2" => "Value 2",
"version" => $version);
echo '<pre>';
print_r($newarray);
echo '</pre>';
foreach ($newarray["version"] as $key => $value) {
print '<br /> key: ' . $key . ' value: ' . $value;
}
You can use foreach($key as $value), when $key equals "version" and do another "for loop" or when $value is type of array, do another "for loop".
Maybe if you look at your arrays in this way it will help :
$version['0'] = 'v1';
$version['1'] = 'v2';
$version['2'] = 'v3';
$newarray['value1'] = 'Value 1';
$newarray['value2'] = 'Value 2';
$newarray['version']['0'] = 'v1';
$newarray['version']['1'] = 'v2';
$newarray['version']['2'] = 'v3';
so to use foreach on version array , the one inside $newarray :
foreach ($newarray['version'] as $key=>$value) {
// Do something
}
I can get the index number from a foreach loop by doing the following.
foreach ($rows as $index=>$row)
{
echo $index.": ".$row;
// gives me "1: $row etc
}
If my array is associative is there away to get the associative name instead of the index number into my loop?
Actually you allready did it:
$associativeArray = array(
'First' => 1,
'Second' => 2,
'Third' => 3,
);
foreach ($associativeArray as $index => $value) {
echo $index . ": " . $value;
}
// First: 1
// Second: 2
// Third: 3
<?
$rows = array();
$rows['hi'] = 'there';
$rows['foo'] = 'bar';
foreach ($rows as $index=>$row)
{
echo $index.": ".$row;
// $index will be hi and foo
}
?>
PHP arrays ARE associative where regular arrays just have integers as keys.
The PHP documentation actually mentions this in the first sentence: http://php.net/manual/en/language.types.array.php
An array in PHP is actually an ordered map..
PHP doesn't have arrays, it has maps/dictionaries that are called arrays but they are not arrays like in other languages.
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 7 years ago.
I've got 2 arrays with the same $key. So arrays are: $users and $new.
$users['user_id']=['user_name'];
$new['user_id']=['user_color'];
How can I foreach them that I could get something like this:
foreach (bla bla bla){
echo '<option color="'.$new['user_color'].'" value="'.$key.'">'.$user['value'].'</option>';
}
You can use a regular foreach loop which treats one array as an associative array, and just get the value corresponding to the key from the other:
foreach ($users as $key => $value) {
echo '<option color="' . $new[$key] . '"value="' . $key . '">'. $value . '</option>';
}
I'm working with Simple HTML DOM like this:
foreach($html->find('img', 18) as $d) {
echo $d->outertext;
}
Now I want to implement an array of variables, in this case images, so I did:
$img=array(
"img"=>"18",
"img"=>"21"
);
foreach($img as $x=>$x_value)
{
$d = $html->find($x, $x_value);
echo $d->outertext;
}
The problem is that Simple HTML DOM is only returning the last image in array, which is number 21. What do I have to do to make it return everything in the array?
It's because both items in your $img array has the same key. foreach doesn't recognize them as two seperate items because both keys are img.
Example code to demonstrate:
$test = array(
"key" => 1,
"key" => 2
);
echo "Length of array: " . count($test) . "\n\n";
echo "Items in array:\n";
foreach($test as $key => $value) {
echo "$key => $value\n";
}
Outputs:
Length of array: 1
Items in array:
key => 2
I have this problem that when I use array as such
Array
(
[11] => /2
[10] => /2
)
I'm unable to get the array name or the value when I use
for($i=0; $i < count($_SESSION['CHECKBOX']);$i++){
how can I get the name? and value? separate?
Given an array $_SESSION['CHECKBOX'], you can use:
foreach($_SESSION['CHECKBOX'] as $key=>$value) {
echo $key . '->' . $value . '<br />';
}
to get the key and values.
Utilize the foreach construct:
The foreach construct provides an easy way to iterate over arrays.
foreach works only on arrays and objects, and will issue an error when
you try to use it on a variable with a different data type or an
uninitialized variable. There are two syntaxes:
It will iterate your array and assign the key to the $key variable and the value to the $value array:
foreach($_SESSION['CHECKBOX'] as $key => $value){
echo "$key = $value";
}
Or concatenate the strings:
echo $key . '=' . $value;