PHP Foreach same key different value [duplicate] - php

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

Related

loop through associative array key [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
I want to loop through an associate array.
foreach($details as $key=>$value){
echo $details['image1'];
}
Above code works fine.
What i want if i can replace the 1 in $details['image1'] to 2,3,4 ..etc
what i tried
$j=i;
foreach($details as $key=>$value){
echo $details['image.$j'];
$j++;
}
But it does not work.
It there a way to dynamically change the key of associate array.
like
'$details['image2'];
$details['image3'];'
You should use double quote mark
$j=i;
foreach($details as $key=>$value){
echo $details["image{$j}"];
$j++;
}
This is one way to do it
$j = $i;
$newArray = [];
foreach ($details as $key => $value) {
$newArray['image'. $j] = $value;
$j++;
}
In echo $details['image.$j']; $j is inserted as a literal.
You can either use
echo $details['image'.$j]; or
echo $details["image{$j}"];
to correctly concat.
Although you actually do not need a foreach loop using this syntax. A simple for-loop would be sufficient.
for ($i = 0; $i < count($details); $i++)
{
echo $details["image.{$i}"];
}
Using foreach you probably do not need to count up $i ... but that depends on your array.
Have a look at https://www.php.net/manual/en/control-structures.foreach.php

How to Loop Array Inside Associative Arrays [duplicate]

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

how can I print each indexed elements of 2D array individually in PHP?

I am trying to solve this problem.
Suppose I have,
$arr = [[1,2,3],[4,5,6],[7,8,9]];
I need the out put like:
1 2 3
4 5 6
7 8 9
Very simply, you could have a nested loop. At the end of the each outer iteration you can print a newline.
<?php
$arr = [[1,2,3],[4,5,6],[7,8,9]];
foreach($arr as $list) {
foreach($list as $element) {
echo $element . ' ';
}
echo '<br>';
}

php adressing variables difference between &$value vs $value [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
What is exactly the difference in php between &$Value and $value in a foreach loop ?
And how it works?
In the example below print_r ($arr) will return the array modified on the first loop and unmodified on the second one.
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $key => &$value) {
$value = $value * 2;
echo "$key => $value; ";
print_r ($arr);
echo '<br>';
}
unset ($value);
unset ($key);
echo '<br>Second loop without "&" on value <br>';
foreach ($arr as $key => $value) {
$value = $value * 2;
echo "$key => $value; " ;
print_r($arr);
echo '<br>';
}
?>
I now it's a beginner question because I'm one :)
Pass the value by-reference instead of by-value. Variables passed by reference (using the reference operator '&') can have their values changed inside of functions.
For example, see the examples here

Multiple array echo with foreach statement in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple index variables in PHP foreach loop
Can we echo multiple arrays using single foreach statement?
Tried doing it in following way but wasn't successful:
foreach($cars, $ages as $value1, $value2)
{
echo $value1.$value2;
}
assuming both arrays have the same amount of elements, this should work
foreach(array_combine($cars, $ages) as $car => $age){
echo $car.$age;
}
if the arrays are not guaranteed to be the same length then you can do something like this
$len = max(count($ages), count($cars));
for($i=0; $i<$len; $i++){
$car = isset($cars[$i]) ? $cars[$i] : '';
$age = isset($ages[$i]) ? $ages[$i] : '';
echo $car.$age;
}
if you just want to join the two arrays, you can do it like this
foreach(array_merge($cars, $ages) as $key => $value){
echo $key . $value;
}

Categories