3v4l.org

run code in 300+ PHP versions simultaneously
<?php $reporting_data = array( 'category_name' => '33287*100*prescription*1,32457*1250*lab*1,32459*1500*lab*1,32460*400*lab*1,32461*600*lab*1,32468*950*lab*1,32470*950*lab*1,33291*2500*lab*1,33292*2500*lab*1,47516*2000*lab*1,49209*0*lab*1,56835*2400*lab*1,56836*2400*lab*1', 'patient' => '28370', 'date' => 1643030497, 'ref' => '371', ); // Create array of objects $reporting_data_as_objects[] = (object)$reporting_data; $results = []; foreach ($reporting_data_as_objects as &$obj) { // Setup base data that is shared across all items $obj->reception_data_sum = 0; $obj->references_data_sum = 0; $obj->actual_price = 0; $category_names = explode(',', $obj->category_name); // Loop over the comma-delimited parts of category_name foreach ($category_names as $category_name) { // Clone our template object $tmp = clone $obj; // The second item of the asterisk-delimted field is the price // We used $_ to indicate that we aren't interested in the first item. list($_, $sale_value) = explode('*', $category_name); // Set object-specific fields on our clone $tmp->category_name = $category_name; $tmp->actual_price = (int)$sale_value; // Add the clone to the array $results[] = $tmp; } } // Always unset by-ref variables of a foreach unset($obj); print_r($results);

preferences:
32.67 ms | 405 KiB | 5 Q