0
protected function _changeArray(&$Products)
    {
        foreach($Products as $item)
        {
            Mage::log('Type: '.$item['attribute']);

            switch($item['shipping_attribute'])
            {
                case '0':
                    $item['is_cargo'] = 'no';
                    $item['is_bulky'] = 'no';
                    $item['is_firework'] = 'no';
                    Mage::log('assigned '.$item['attribute']);
                    break;
                case '1':
                    $item['is_cargo'] = 'yes';
                    $item['is_bulky'] = 'no';
                    $item['is_firework'] = 'no';
                    Mage::log('assigned '.$item['attribute']);
                    break;
                case '2':
                    $item['is_cargo'] = 'no';
                    $item['is_bulky'] = 'yes';
                    $item['is_firework'] = 'no';
                    Mage::log('assigned '.$item['attribute']);
                    break;
                case '3':
                    $item['is_cargo'] = 'no';
                    $item['is_bulky'] = 'no';
                    $item['is_firework'] = 'yes';
                    Mage::log('assigned '.$item['attribute']);
                    break;
            }
            Mage::log('"switch" finished!');
        }
    }

I am trying to change these elements of the Products array, depending on the value of another element of the array. What seems not to happen is they don't get changed... Everything always stays to 'no' value. I know that the 'attribute' value is different in a correct way and that the function goes through the switch-cases correctly, because of the testings that I am writing in the log.

I am printing the Products array after calling my function, and it is not changed. I thought it will work with passing by reference, but nope... I also tried with passing the array the normal way and returning it from the function - unsuccessful again.

What am I doing wrong or not doing enough ?

Oh, yeah, I also tried having the switch-case in the following way:

case '...':
    $Products['is_cargo'] = 'yes';
    $Products['is_bulky'] = 'no';
    $Products['is_firework'] = 'no';
    Mage::log('assigned '.$Products['attribute']);
    break;

nope again...

1
  • foreach($Products as &$item) Commented Oct 16, 2013 at 9:42

2 Answers 2

2

You need to set $item "by reference" in your foreach loop

foreach($Products as &$item)
Sign up to request clarification or add additional context in comments.

Comments

1

You do not actually do anything to the input array, except for reading them..

You need to change the input array, not the $item array, $item is only an extracted item.

So basically:

use:

 foreach($Products as $key => $item)

and change to:

$Products[$key]['is_cargo'] = 'no';

Ideally you do not use the function by reference, but return the output array from the function to the script

Added:

I would run it like this:

protected function _changeArray($Products)
{
    foreach($Products as $key => $product)
    {
        Mage::log('Type: '.$Products[$key]['attribute']);

        switch($Products[$key]['shipping_attribute'])
        {

            case '0':
                $Products[$key]['is_cargo'] = 'no';
                $Products[$key]['is_bulky'] = 'no';
                $Products[$key]['is_firework'] = 'no';
                break;
//etc

and do a return $products at the end of the function.

Call:

$data = _changeArray($data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.