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?

4
  • 1
    How do you use array_column()? 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() operates on the second dimension only... Commented Jan 9, 2018 at 23:33
  • 1
    Are you running it on PHP v7.0.0? That's when support for an array of objects was added. Commented Jan 9, 2018 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. Commented Jan 9, 2018 at 23:36
  • As @DanMan mentioned, this should work fine for PHP 7 only. See: 3v4l.org/nCqWZ for a similar array. What version of PHP do you use? Commented Jan 10, 2018 at 0:01

2 Answers 2

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

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

1 Comment

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.