2

I'm trying modify a CVS price list export file by passing a url variable defining the currency exchange rate.

But if I try add variables to the array_map function, it says the variable are undefined, even though they are set higher up in the scope.

I didn't build this price list export file, but the developer is unable to help me at this time so any thoughts would be much appreciated.

See code below with comments...

If you read my comments below the code it might make the question more understandable.

// this is my common function class to get url variable
$sCurrency = CF::getVar('currency');

// this is my exchange rate from my site settings
$iEuro = (int)get_field('currency_rate_eur','option');

// get our posts
$posts = get_posts([
    'post_type' => 'product'
]);

// create empty array of data
$data = [];

// loop through our posts
foreach ($posts as $post) {

    $data[$post->ID] = [
        'quantities' => get_field('product_prices', $post_id)
    ];

}

// format our quantities for every item
{

    // more code here but not included for this question

    // map our data to include our structured quantities
    $data = array_map(function ($row) use ($quantities) {

        // more code here but not included for this question

        // combine all of our values
        $row['quantities'] = array_combine(
            array_map(function ($row) {
                return $row['quantity'];
            }, $row['quantities']),
            array_map(function ($row) {
                return $row['unit_price'];
            }, $row['quantities'])
        );

        return $row;

    }, $data);
}

In the code above, you will see the combined array function with array_map functions within it.

I'm trying to pass variables to the array_map that returns $row['unit_price'].

This is so I can multiply the $row['unit_price'] by the url var defining the exchange rate.

But every time I try to use a variable inside the array_map function, I get an error saying var is undefined.

This has been my attempt below, but keep getting undefined variable errors...

array_map(function ($row) {

    // set unit price var
    $iUnitPrice = $row['unit_price'];

    // if currency var is defined as euro
    if($sCurrency === 'eur') {

        // then multiply the unit price by the euro exchange rate
        $iUnitPrice = $iUnitPrice * $iEuro;

    }

    // return the appropriate unit price
    return $iUnitPrice;

}, $row['quantities'])

I know I will need to ceil these returned value, but I would like to know why array_map excludes these vars from the scope.

Thanks in advance.

Nick
  • 138,499
  • 22
  • 57
  • 95
joshmoto
  • 4,472
  • 1
  • 26
  • 45
  • 2
    The solution is exactly the same as in your other `array_map`, you need a `use ($sCurrency, $iEuro)` after `function ($row)` – Nick May 08 '19 at 00:21
  • Nice... I get ya. I'm unfamiliar with `use` but i think I know what you mean, let try it out – joshmoto May 08 '19 at 00:23
  • 1
    In your other call to `array_map` you have `function ($row) use ($quantities) {`, this lets you use `$quantities` inside that function. The same applies to this one, you just need `function ($row) use ($sCurrency, $iEuro) {` to get those variables defined inside the function body. – Nick May 08 '19 at 00:25
  • You nailed it @nick thank you mate. Appreciate you taking a look for me. Leave an answer i'll check it as answered. Really much appreciated! – joshmoto May 08 '19 at 00:33
  • Because the `array_map` was within another `array_map`, I had to pass the vars using `use` twice but still worked a beaut! – joshmoto May 08 '19 at 00:36
  • 1
    That makes sense, you have to pass them all the way up the scope... – Nick May 08 '19 at 00:44

0 Answers0