-1

I have a multidimensional array with a race type column and a boats column (which has the total number of boats for each race type). This is how I am getting the array values:

$boats = $wpdb->get_results("
        select rt.race_type
              ,sum(tr.boat_count) boats
          from registrations tr
         group by rt.race_type;
");

The array works perfectly fine. But now I am trying to get the total number of boats for all race types (without using a loop). After some research, I have tried the code below, but it doesn't seem to be working:

$totalboats = array_sum(array_column($boats,'boats'));

When I run the following command:

echo $totalboats;

The result of that is 0, which is clearly wrong.

Any ideas? I am running PHP 5.6.29.

================== EDIT 01 ==================

As requested, here is the var_dump of $boats:

array(2) {
    [0]=>
        object(stdClass)#672 (2) {
            ["race_type"]=> string(12) "Elite 8-Hour"
            ["boats"]=> string(1) "2"
        }
    [1]=>
        object(stdClass)#673 (2) {
            ["race_type"]=> string(12) "Sport 4-Hour"
            ["boats"]=> string(1) "2"
        }
}
9
  • 2
    Could you please show the output of var_dump($boats); ? Commented Mar 23, 2017 at 13:19
  • It should be sum(tr.boat_count) AS boats, not sum(tr.boat_count) boats. Commented Mar 23, 2017 at 13:23
  • 1
    Per the documentation on $wpdb->get_results - it appears this method may actually hydrate into objects as opposed to a multidimensional array. codex.wordpress.org/Class_Reference/wpdb Commented Mar 23, 2017 at 13:26
  • 1
    @arkascha It works just as well without the AS keyword. Commented Mar 23, 2017 at 13:27
  • try this. first call array_column() funtion and store in to a variable and then call array_sum() function, without calling one function inside another function. Commented Mar 23, 2017 at 13:34

2 Answers 2

2

The problem is that your $boats sub-elements are objects and not arrays. array_column doesn't work with objects in PHP5 (it does in PHP7, though).

You could use a workaround using array_map, as shown in this answer to a previous question :

$totalboats = array_sum(
    array_column(
        array_map(
            function($o){
                return (array)$o;
            },
            $boats),
       'boats')
    );
echo $totalboats; // echoes "4"
Sign up to request clarification or add additional context in comments.

2 Comments

At this level of complexity, you might as well use a foreach() and dispense with the type casting and array_map()
Agreed. Still, the original question stated without using a loop, so I took it as a challenge ;)
0

If your variable is array and not object you can use this

$array = [
  0 => [
     'race_type' => 'Elite 8-Hour',
     'boats' => '2',
    ],
  1 => [
     'race_type' => 'Sport 4-Hour',
     'boats' => '2',
    ]
];



$toSum = array_sum(array_column($array, 'boats'));

echo "<pre>";print_r($toSum);die;

1 Comment

You mean he can use exactly what he's already using ? Shocking.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.