0

I know that was ask many times but I didn't find something related to me. I'm new to PHP and I'm learning how to use foreach right now. I got this code:

foreach($revert_result as $tu => $row){
    $address = $row['Addr.Line2'] . $row['Addr.zip'] . $row['Addr.City'];
}

$row['Addr.Line2'] etc. works good, it display the information I want but only the last element. So the last element overwrites all the other elements and I don't know why.

Note that $row['Addr.Line2'] etc. are datas from an ERP system. This should be correct because it can display the datas.

Can someone explain me why it is or give me a hint how to fix?

Thanks in advance

2
  • You need to include the $revert_result as well, it is not a foreach issue but how you're fetching the data. Commented Dec 4, 2013 at 16:06
  • What is your expected result? You overwrite $address on each loop iteration, so it will always contain only the last value for $row when inspected after the loop. Commented Dec 4, 2013 at 16:07

5 Answers 5

3
foreach($revert_result as $tu => $row){
    $address = $row['Addr.Line2'] . $row['Addr.zip'] . $row['Addr.City'];
}

Your foreach loop will loop through the $revert_result array, and store the concatenated string in $address variable. The variable value is overwritten on each iteration of the loop, and after the loop finishes executing, the $address variable will contain the value of the last iteration.

I believe you need all the values here. In that case, you add the values into an array, like so:

$addresses[] = array(); // initialize empty array

foreach($revert_result as $tu => $row){
    $addresses[] = $row['Addr.Line2'] . $row['Addr.zip'] . $row['Addr.City'];
}

Now $addresses will contain all the address values and you can use it however you wish.

Sign up to request clarification or add additional context in comments.

Comments

2

This should work:

// declare an empty string variable to hold your addresses
// sometimes PHP error reporting will throw a fit if you try to use $address improperly later
$address = '';

// loop through your results
foreach($revert_result as $tu => $row){

    // concatenate the addresses and add a <br> at the end to make the output legible
    $address.= $row['Addr.Line2'] . $row['Addr.zip'] . $row['Addr.City'] . '<br>';

}

// display your output
echo $address;

Or this:

// declare an array to store your addresses
// sometimes PHP error reporting will throw a fit if you try to use $address improperly later
$address = array();

// loop through your results
foreach($revert_result as $tu => $row){

    // add a concatenated result item to your array
    // calling $address[] automatically assigns to the next index
    // the first loop will do $address[0]
    // the second loop will do $address[1]
    // the third loop will do $address[2]
    $address[] = $row['Addr.Line2'] . $row['Addr.zip'] . $row['Addr.City'];

}

// use print_r() to nicely display all elements of your array
echo '<pre>'.print_r($address, true).'</pre>';

// You can also loop the $address array and echo
foreach($address as $k=>$v){
    echo $v.'<br>';
}

Comments

1

You might want to use an array to save your info instead:

$address = array();
foreach($revert_result as $tu => $row){
    $address[] = $row['Addr.Line2'] . $row['Addr.zip'] . $row['Addr.City'];
}
print_r($address);

Comments

1

On the first iteration of the loop, the Addr parts of the first row of $revert_result are assigned to $address. On the second iteration of the loop, the Addr parts of the second row of $revert_result are assigned to $address. On the third iteration... you get the idea.

You're repeatedly overwriting the same variable with a new value, of course only the last one will remain after the loop. Perhaps you want to create an array instead:

$addresses = array();
foreach (...) {
    $addresses[] = $row[...];
}

Comments

0

I had a similar issue also, but I was saving an object in each element of an array on each iteration of the foreach loop. My array for storing the new values was incremented by an index like $myArray[$index](But as I learnt from previous answers I can use $myArray[]) so I expected each value to be different, but all elements were the same as the last value.

It turned out every element of my array the object was saved in, that value would update when I made a change to the object.

To fix this I had to unset($myObject) after assigning a new value to it and first thing in the foreach loop, I had to instantiate a new object of its kind from the class with $myObject = new myObject;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.