3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Example { public $length, $height, $width, $weight; public function __construct($length, $height, $width, $weight) { $this->length = $length; $this->height = $height; $this->width = $width; $this->weight = $weight; } public function get_price() { return 1.23; } } $sample_data = [ 42 => ['quantity' => 2, 'data' => new Example(42, 42, 42, 42)], 69 => ['quantity' => 1, 'data' => new Example(69, 69, 69, 69)], ]; $products = []; $i = 0; $totalweight = 0; $totalprice = 0; foreach ( $sample_data as $item_id => $values ) { $_product = $values['data']; $q = 1; while ( $values['quantity'] >= $q ) { $products[ $i ] = new stdClass; $products[ $i ]->length = $_product->length; $products[ $i ]->height = $_product->height; $products[ $i ]->width = $_product->width; $products[ $i ]->weight = $_product->weight; $totalweight += $_product->weight; $totalprice += $_product->get_price(); $q++; $i++; } } var_dump($products);
Output for git.master, git.master_jit, rfc.property-hooks
array(3) { [0]=> object(stdClass)#3 (4) { ["length"]=> int(42) ["height"]=> int(42) ["width"]=> int(42) ["weight"]=> int(42) } [1]=> object(stdClass)#4 (4) { ["length"]=> int(42) ["height"]=> int(42) ["width"]=> int(42) ["weight"]=> int(42) } [2]=> object(stdClass)#5 (4) { ["length"]=> int(69) ["height"]=> int(69) ["width"]=> int(69) ["weight"]=> int(69) } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
109.65 ms | 406 KiB | 5 Q