array_column不适用于对象数组?

时间:2018-01-09 23:28:55

标签: php arrays multidimensional-array array-column

这是我目前的数组结构:

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"
    }
  }
}

每次为array_column运行max_weight时,我都会得到一个空数组。我应该将返回的数组用于max()。 有解决方法吗?

2 个答案:

答案 0 :(得分:2)

  

v7.0.0:添加了输入参数为对象数组的功能。

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

答案 1 :(得分:1)

在PHP 7中array_column对我来说正常工作。这是一个例子......

<?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;
?>