Skip to content

Instantly share code, notes, and snippets.

@kristoferdoman
Last active August 26, 2016 18:39
Show Gist options
  • Save kristoferdoman/535348f64273db974addb668579ddf03 to your computer and use it in GitHub Desktop.
Save kristoferdoman/535348f64273db974addb668579ddf03 to your computer and use it in GitHub Desktop.
Php: Sum Property of an Object Collection in a Nested Object Array
<?php
$object = new Object();
$count = array_sum(array_map(function($x) {
$count = array_sum(array_map(function($y) { return count($y->propertyOfAnotherObject); }, $x->anotherObjectInsideObject));
return $count;
}, $object));
?>
@kristoferdoman
Copy link
Author

Example: You can use this code to get the sum of all value properties, in every list object, inside every object "object".

$objects = array();

class Object {
    public list = array();
}

class List {
    public value = null;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment