Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP referencing array instead of copying
    primarykey
    data
    text
    <p>I have an array, I need to copy the first item in the array and alter it.</p> <p>This is what i'm doing:</p> <pre><code>echo $QuantityDiscounts[0]['price'] . '&lt;br&gt;'; echo $QuantityDiscounts[0]['from_quantity'] . '&lt;br&gt;'; $firstItem = $QuantityDiscounts[0]; $firstItem['from_quantity'] = 999; $firstItem['price'] = 999; echo $QuantityDiscounts[0]['price'] . '&lt;br&gt;'; echo $QuantityDiscounts[0]['from_quantity'] . '&lt;br&gt;'; </code></pre> <p>This is the output this gives me:</p> <pre><code>4.870000 10 4.870000 999 </code></pre> <p>When I change the value of the copied array it's changing the original array. What makes this even stranger is that it only happens for the 'from_quantity' item. As you can see the 'price' element remains unaltered.</p> <p>I can't figure out why this is happening, as you can see i'm not using references. Is there an explanation for this behaviour that i'm missing?</p> <p>Some more info:</p> <p>If I first copy the 'from_quantity' in the original array so that it uses a different key this behaviour goes away.</p> <pre><code>$QuantityDiscounts[0]['test'] = $QuantityDiscounts[0]['from_quantity']; echo $QuantityDiscounts[0]['price'] . '&lt;br&gt;'; echo $QuantityDiscounts[0]['from_quantity'] . '&lt;br&gt;'; echo $QuantityDiscounts[0]['test'] . '&lt;br&gt;'; $firstItem = $QuantityDiscounts[0]; $firstItem['from_quantity'] = 999; $firstItem['test'] = 999; $firstItem['price'] = 999; echo $QuantityDiscounts[0]['price'] . '&lt;br&gt;'; echo $QuantityDiscounts[0]['from_quantity'] . '&lt;br&gt;'; echo $QuantityDiscounts[0]['test'] . '&lt;br&gt;'; </code></pre> <p>Outputs:</p> <pre><code>4.870000 10 10 4.870000 999 10 </code></pre> <p>** UPDATE ** - Thanks for your help so far</p> <p>Here is the function that generates the array. I can see that a reference is being used there which must be causing the problems. Does this mean I can't copy and modify the 'from_quantity' without changing the original?</p> <pre><code>protected function formatQuantityDiscounts($specific_prices, $price, $tax_rate, $ecotax_amount) { foreach ($specific_prices as $key =&gt; &amp;$row) { $row['quantity'] = &amp;$row['from_quantity']; if ($row['price'] &gt;= 0) // The price may be directly set { $cur_price = (Product::$_taxCalculationMethod == PS_TAX_EXC ? $row['price'] : $row['price'] * (1 + $tax_rate / 100)) + (float)$ecotax_amount; if ($row['reduction_type'] == 'amount') $cur_price -= (Product::$_taxCalculationMethod == PS_TAX_INC ? $row['reduction'] : $row['reduction'] / (1 + $tax_rate / 100)); else $cur_price *= 1 - $row['reduction']; $row['real_value'] = $price - $cur_price; } else { if ($row['reduction_type'] == 'amount') $row['real_value'] = Product::$_taxCalculationMethod == PS_TAX_INC ? $row['reduction'] : $row['reduction'] / (1 + $tax_rate / 100); else $row['real_value'] = $row['reduction'] * 100; } $row['nextQuantity'] = (isset($specific_prices[$key + 1]) ? (int)$specific_prices[$key + 1]['from_quantity'] : -1); } return $specific_prices; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload