0

So i have this function that works great at adding up the sum of all values of a specific field in an array, in this case num_sold

So the array is something like this

array(4) {
  [0]=>
  array(2) {
    ["num_sold"]=>
    string(1) "6"
    ["id_product"]=>
    string(1) "2"
  }
  [1]=>
  array(2) {
    ["num_sold"]=>
    string(1) "6"
    ["id_product"]=>
    string(1) "3"
  }
  [2]=>
  array(2) {
    ["num_sold"]=>
    string(1) "4"
    ["id_product"]=>
    string(1) "4"
  }
  [3]=>
  array(2) {
    ["num_sold"]=>
    string(1) "5"
    ["id_product"]=>
    string(1) "7"
  }
}

// this correctly returns 21
$total = array_sum(array_map(
        function($element){
            return $element['num_sold'];
        },
        $array));

Now i want to be able to reuse this function on other field names so i wanted to create a function however the $field value isn't getting through. In php storm it says the $field value in sumFieldArray($field,$array) is not used anywhere but its clearly in the function.

I'm guessing it might be a scope issue so i tried doing global $field on the first line but that made no difference and instead it now says the global $field is not used anywhere. What am i doing wrong.

public static function sumFieldArray($field,$array)
    {
        $field_sum = array_sum(array_map(
            function($element, $field){
                return $element[$field];
            },
            $array));
        return $field_sum;
    }
3
  • php has exactly two scopes: global (top-level of your code), and local - within the current block. a variable defined at some intermediate level is essentially totally inaccessible, since global goes ONLY to the absolute top-level scope. there's no parent $var or grandparent $var scoping ability. Commented Dec 10, 2015 at 21:43
  • @MarcB You're forgetting the use() option in function expressions. Commented Dec 10, 2015 at 21:43
  • @barmar: ah yeah, true enough. but that's something that has to be done in the parent scope. a child can't reach back and fetch that stuff. Commented Dec 10, 2015 at 21:45

2 Answers 2

2

You are right, this is a scope issue. You have to inherit parent scope with use.

public static function sumFieldArray($field, $array)
{
    $field_sum = array_sum(array_map(
        function($element, $field) use ($field) {
            return $element[$field];
        },
        $array));
    return $field_sum;
}

More information here: http://php.net/manual/en/functions.anonymous.php

Sign up to request clarification or add additional context in comments.

Comments

2

array_sum() calls the function with only one argument, the array element. It won't pass $field as the second argument. You can use the use() option to allow the inner function to access a variable in the outside scope.

public static function sumFieldArray($field,$array)
    {
        $field_sum = array_sum(array_map(
            function ($element) use($field) {
                return $element[$field];
            },
            $array));
        return $field_sum;
    }

If you're using PHP 5.5+, you can also use the array_column function to extract all the values of that field.

public static function sumFieldArray($field,$array)
    {
        return array_sum(array_column($array, $field));
    }

2 Comments

This would have been my preferred answer since it gives that useful array_column version even though that didn't work for me as i dont have php 5.5 others will find it useful however that code isnt valid. In php storm it says it expects ( after function so doing "function use($field) ($element){" will not work. I imagine it just needs a quick tweak and it will be a useful answer, appreciate the help.
I had the use option in the wrong place, it goes after the argument list.

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.