13

Why does this yield this:

foreach( $store as $key => $value){
$value = $value.".txt.gz";
}

unset($value);

print_r ($store);

Array
(
[1] => 101Phones - Product Catalog TXT
[2] => 1-800-FLORALS - Product Catalog 1
)

I am trying to get 101Phones - Product Catalog TXT.txt.gz

Thoughts on whats going on?

EDIT: Alright I found the solution...my variables in my array had values I couldn't see...doing

$output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
echo($output);

Cleaned it up and made it work properly

0

9 Answers 9

41

The doc http://php.net/manual/en/control-structures.foreach.php clearly states why you have a problem:

"In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference."

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>

Referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won't work:

<?php
/** this won't work **/
foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I am still getting this when I follow your first example: Array ( .txt.gz => 101Phones - Product Catalog TXT .txt.gz => 1-800-FLORALS - Product Catalog 1 ) why is it assigning my key as the text I am adding to the variable?
if anyone happens to stumble into this like I did after me, this is the solution to this question most of the time. OP, consider marking this as the right answer, if you ever login again...
6

Try

foreach( $store as $key => $value){
    $store[$key] = $value.".txt.gz";
}

Comments

6

The $value variable in the array is temporary, it does not refer to the entry in the array.
If you want to change the original array entry, use a reference:

foreach ($store as $key => &$value) {
                       //  ^ reference
    $value .= '.txt.gz';
}

3 Comments

When I do that it gives me: Array ( .txt.gz => 101Phones - Product Catalog TXT .txt.gz => 1-800-FLORALS - Product Catalog 1 )
It should certainly not do that. Post the full code that produces that result.
I just realized it is something weird from my variables ....I created an array $stores=array("tree","boat"); and it worked...whats a good way to remove unseen characters?
4

You are rewriting the value within the loop, and not the key reference in your array.

Try

 $store[$key] = $value.".txt.gz";

Comments

3

pass $value as a reference:

foreach( $store as $key => &$value){
   $value = $value.".txt.gz";
}

Comments

3

Try

$catalog = array();

foreach( $store as $key => $value){
    $catalog[] = $value.".txt.gz";
}


print_r ($catalog);

OR

foreach( $store as $key => $value){
    $store[$key] = $value.".txt.gz";
}


print_r ($store);

Depends on what you want to achieve

Thanks :)

1 Comment

Its omitting the .txt.gz Array ( [1] => 101Phones - Product Catalog TXT [2] => 1-800-FLORALS - Product Catalog 1 )
2

I believe this is what you want to do:

foreach( $store as $key => $value){
$store[$key] = $value.".txt.gz";
}

unset($value);

print_r ($store);

Comments

2

How about array map:

$func = function($value) { return $value . ".txt.gz"; };
print_r(array_map($func, $store));

Comments

2
foreach(array_container as & array_value)

Is the way to modify array element value inside foreach loop.

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.