12

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

user1179295
  • 706
  • 3
  • 10
  • 21

9 Answers9

40

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;
}
?>
Michel
  • 950
  • 14
  • 23
  • 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? – user1179295 Mar 29 '12 at 07:46
  • 2
    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... – RaKXeR May 20 '16 at 14:41
6

Try

foreach( $store as $key => $value){
    $store[$key] = $value.".txt.gz";
}
Andy
  • 2,095
  • 1
  • 29
  • 59
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';
}
deceze
  • 510,633
  • 85
  • 743
  • 889
  • When I do that it gives me: Array ( .txt.gz => 101Phones - Product Catalog TXT .txt.gz => 1-800-FLORALS - Product Catalog 1 ) – user1179295 Mar 29 '12 at 07:36
  • It should certainly not do that. Post the full code that produces that result. – deceze Mar 29 '12 at 07:58
  • 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? – user1179295 Mar 29 '12 at 08:04
4

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

Try

 $store[$key] = $value.".txt.gz";
Daniel
  • 3,726
  • 4
  • 26
  • 49
3

pass $value as a reference:

foreach( $store as $key => &$value){
   $value = $value.".txt.gz";
}
k102
  • 7,861
  • 7
  • 49
  • 69
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 :)

Baba
  • 94,024
  • 28
  • 166
  • 217
  • Its omitting the .txt.gz Array ( [1] => 101Phones - Product Catalog TXT [2] => 1-800-FLORALS - Product Catalog 1 ) – user1179295 Mar 29 '12 at 07:58
2
foreach(array_container as & array_value)

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

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
2

How about array map:

$func = function($value) { return $value . ".txt.gz"; };
print_r(array_map($func, $store));
Jarosław Gomułka
  • 4,935
  • 18
  • 24
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);
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65