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

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

Related

PHP Foreach same key different value [duplicate]

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

PHP use current function in foreach [duplicate]

This question already has answers here:
How does PHP 'foreach' actually work?
(7 answers)
Closed 8 years ago.
$arr = array(1,2,3,4,5);
foreach($arr as $key => $row) {
echo current($arr);
}
//output is 22222, why?
Why the result is not 12345?
if you want the output to be 12345:
$arr = array(1,2,3,4,5);
foreach($arr as $key => $val) {
echo $val;
}

php foreach Loop try not to copy a array [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(27 answers)
Closed 9 years ago.
Is there any way of not copying the specific array in the foreach loop?
Here's the code
<?php
$letters = array("A","B","B","C");
foreach ($letters as $char){
if ($char == "B") {
continue;
}
echo $char;
}
?>
I want my output to be only ABC not AC
You could strip non-unique elements first:
foreach(array_unique($letters) AS $char)
To copy array use
$a = array("A","B","B","C");
$b = array_unique($a); // $b will be a different array with unique values
There is no need to use foreach. In PHP by default variables are not assigned by reference but by the value unless you use & operator.
The other way is to use array_merge()
$a = array("A","B","B","C");
$b = array();
$b = array_merge(array_unique($a), $b);
In both cases the result will be A B C
Try like
<?php
$letters = array("A","B","B","C");
$letters = array_unique($letters);
foreach ($letters as $char){
echo $char;
}
?>

How can I iterate through two arrays at the same time without re-iterating through the parent loop? [duplicate]

This question already has answers here:
php looping through multiple arrays [duplicate]
(8 answers)
Closed 9 years ago.
How can I iterate through two arrays at the same time that have equal sizes ?
for example , first array $a = array( 1,2,3,4,5);
second array $b = array(1,2,3,4,5);
The result that I would like through iterating through both is having the looping process going through the same values to produce a result like
1-1
2-2
3-3
4-4
5-5
I tried to do it this way below but it didn't work , it keeps going through the first loop again
foreach($a as $content) {
foreach($b as $contentb){
echo $a."-".$b."<br />";
}
}
Not the most efficient, but a demonstration of SPL's multipleIterator
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($a));
$mi->attachIterator(new ArrayIterator($b));
$newArray = array();
foreach ( $mi as $value ) {
list($value1, $value2) = $value;
echo $value1 , '-' , $value2 , PHP_EOL;
}
Use a normal for loop instead of a foreach, so that you get an explicit loop counter:
for($i=0; $i<count($content)-1; $i++) {
echo $content[$i].'-'.$contentb[$i];
}
If you want to use string based indexed arrays, and know that the string indexes are equal between arrays, you can stick with the foreach construct
foreach($content as $key=>$item) {
echo $item.'-'.$contentb[$key];
}
If they're the same size, just do this:
foreach($a as $key => $content){
$contentb = $b[$key];
echo($content."-".$contentb."<br />");
}

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