2

This is my current array structure:

array(2) {
  ["Ground"] => array(4) {
    [0] => object(Shipping\Model\Shipping)#337 (5) {
      ["shipping_id"] => NULL
      ["min_weight"] => string(4) "0.00"
      ["max_weight"] => string(4) "5.00"
      ["shipping_method"] => string(6) "Ground"
      ["shipping_rate"] => string(4) "8.00"
    }
    [1] => object(Shipping\Model\Shipping)#385 (5) {
      ["shipping_id"] => NULL
      ["min_weight"] => string(4) "6.00"
      ["max_weight"] => string(5) "10.00"
      ["shipping_method"] => string(6) "Ground"
      ["shipping_rate"] => string(5) "12.00"
    }
  }
  ["Expedited"] => array(4) {
    [0] => object(Shipping\Model\Shipping)#388 (5) {
      ["shipping_id"] => NULL
      ["min_weight"] => string(4) "0.00"
      ["max_weight"] => string(4) "5.00"
      ["shipping_method"] => string(9) "Expedited"
      ["shipping_rate"] => string(5) "12.00"
    }
  }
}

Every time I run array_column for max_weight I get an empty array as a result. I'm supposed to use the returned array for max(). Is there a workaround for this?

herondale
  • 729
  • 10
  • 27
  • 1
    How do you use [`array_column()`](http://php.net/manual/en/function.array-column.php)? Taking the objects into account, your array is tri-dimensional and you are trying to get a value from the third dimension while [`array_column()`](http://php.net/manual/en/function.array-column.php) operates on the second dimension only... – axiac Jan 09 '18 at 23:33
  • 1
    Are you running it on PHP v7.0.0? That's when support for an array of objects was added. – DanMan Jan 09 '18 at 23:33
  • @axiac I use `array_column()` like this --> `max(array_column(($this->shippingMethods[$shippingMethod]), 'max_weight'));` where `$this->shippingMethods[$shippingMethod]` is either the `[Ground]` array or `[Expedited]` array. – herondale Jan 09 '18 at 23:36
  • As @DanMan mentioned, this should work fine for PHP 7 only. See: https://3v4l.org/nCqWZ for a similar array. What version of PHP do you use? – jh1711 Jan 10 '18 at 00:01

2 Answers2

4

v7.0.0: Added the ability for the input parameter to be an array of objects.

Source: https://secure.php.net/manual/en/function.array-column.php

DanMan
  • 11,323
  • 4
  • 40
  • 61
  • I just checked my PHP version, I thought I was running it on 7, turns out I was running it on 5.6. So I guess my resort is a loop then, no? – herondale Jan 10 '18 at 01:19
1

The array_column is working correctly for me in PHP 7. Here is an example ...

<?php
$shippingMethods = array("Ground" => array());

$gMethod = new stdClass();
$gMethod->shipping_id = NULL;
$gMethod->min_weight = "0.00";
$gMethod->max_weight = "5.00";
$gMethod->shipping_method = "Ground";
$gMethod->shipping_rate = "8.00";

$shippingMethods["Ground"][] = $gMethod;

$gMethod = new stdClass();
$gMethod->shipping_id = NULL;
$gMethod->min_weight = "6.00";
$gMethod->max_weight = "10.00";
$gMethod->shipping_method = "Ground";
$gMethod->shipping_rate = "12.00";

$shippingMethods["Ground"][] = $gMethod;

$shippingMethod = "Ground";
$result = max(array_column(($shippingMethods[$shippingMethod]), "max_weight"));

echo $result;
?>
Brian Otto
  • 56
  • 6